How to Get Current Time and Date in C++

How to get the date and time values in a C program?

Use time() and localtime() to get the time:

#include <stdio.h>
#include <time.h>

int main()
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("now: %d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
}

Get the current time in C

Copy-pasted from here:

/* localtime example */
#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );

return 0;
}

(just add void to the main() arguments list in order for this to work in C)

How to get current time and date in C++?

Since C++ 11 you can use std::chrono::system_clock::now()

Example (copied from en.cppreference.com):

#include <iostream>
#include <chrono>
#include <ctime>

int main()
{
auto start = std::chrono::system_clock::now();
// Some computation here
auto end = std::chrono::system_clock::now();

std::chrono::duration<double> elapsed_seconds = end-start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);

std::cout << "finished computation at " << std::ctime(&end_time)
<< "elapsed time: " << elapsed_seconds.count() << "s"
<< std::endl;
}

This should print something like this:

finished computation at Mon Oct  2 00:59:08 2017
elapsed time: 1.88232s

most efficient way to get current time/date/day in C

Standard C provides only one way to get the time - time() - which can be converted to a time/date/year with localtime() or gmtime(). So trivially, that must be the most efficient way.

Any other methods are operating-system specific, and you haven't told us what operating system you're using.

How to get the current time and date C++ UTC time not local

You are looking for the gmtime function in the time.h library, which give you UTC time. Here's an example:

#include <stdio.h>      /* printf */
#include <time.h> /* time_t, struct tm, time, gmtime */

int main ()
{
time_t rawtime;
struct tm * ptm;
// Get number of seconds since 00:00 UTC Jan, 1, 1970 and store in rawtime
time ( &rawtime );
// UTC struct tm
ptm = gmtime ( &rawtime );
// print current time in a formatted way
printf ("UTC time: %2d:%02d\n", ptm->tm_hour, ptm->tm_min);

return 0;
}

Look at these sources:

  • https://www.cplusplus.com/reference/ctime/gmtime/
  • https://www.cplusplus.com/reference/ctime/time_t/

C Program to print Current Time

I think your looking for something like this:

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

int main() {

time_t current_time;
char* c_time_string;

current_time = time(NULL);

/* Convert to local time format. */
c_time_string = ctime(¤t_time);

printf("Current time is %s", c_time_string);

return 0;
}

How do I get the current date in C++?

You can try below code and description beneath it.

#include <iostream>
#include <ctime>

int main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer[80];

time (&rawtime);
timeinfo = localtime(&rawtime);

strftime(buffer,80,"%d-%m-%Y %I:%M:%S",timeinfo);
std::string str(buffer);

std::cout << str;

return 0;
}

Function

time_t time (time_t* timer);

function returns this value, and if the argument is not a null pointer, it also sets this value to the object pointed by timer.

Parameters

  • timer
    Pointer to an object of type time_t, where the time value is stored.you can also pass it null pointer in case not required

Return Value

The current calendar time as a time_t object.If the function could not retrieve the calendar time, it returns a value of -1.

Getting Current Date and Time into 2 different variables in C Programming

You're using the wrong format string for the time.

The %I format specifier gives you the hour in the range 01-12. So %H:%I gives you the hour twice: first in 24 hour format then 12 hour format.

If you want minutes and seconds, you need to use %M and %S respectively.

strftime(current_time, sizeof(current_time), "%H:%M:%S", time_ptr);

How can i get the current time in C with d/m/y format?

Like this:

int main ()
{
time_t rawtime;
struct tm * currentTime;
time ( &rawtime );
currentTime = localtime ( &rawtime );
printf ( "%d/%d/%d", currentTime->tm_mday, currentTime->tm_mon+1, currentTime->tm_year+1900);

return 0;
}

Be careful, months are indexed since 0, and year is since 1900 in tm struct.

Using ctime to print current system time to millisecond precision?

On most platforms you can take the second part of a struct timespec (tv_sec) gotten from timespec_get and use localtime or gmtime to break it down into its components, leaving only the nano second part.

#include <time.h>
#include <stdio.h>

int main() {
struct timespec ts;
timespec_get(&ts, TIME_UTC);
time_t seconds = ts.tv_sec;

printf("%s", ctime(&seconds)); // just for comparison

struct tm *t = localtime(&seconds);

printf("%04d-%02d-%02dT%02d:%02d:%02d.%09ld\n",
t->tm_year+1900, t->tm_mon+1, t->tm_mday,
t->tm_hour, t->tm_min, t->tm_sec,
ts.tv_nsec
);
}

Possible output:

Fri May 27 18:36:14 2022
2022-05-27T18:36:14.513916611

If you only need milliseconds:

    printf("%04d-%02d-%02dT%02d:%02d:%02d.%03ld\n",
t->tm_year+1900, t->tm_mon+1, t->tm_mday,
t->tm_hour, t->tm_min, t->tm_sec,
ts.tv_nsec / 1000000
);

Possible output:

Fri May 27 18:36:14 2022
2022-05-27T18:36:14.513


Related Topics



Leave a reply



Submit