// C++ Program to print current Day, Date and Time
#include <ctime>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
// Declaring argument for time()
time_t tt;
// Declaring variable to store return value of
// localtime()
struct tm* ti;
// Applying time()
time(&tt);
// Using localtime()
ti = localtime(&tt);
cout << "Current Day, Date and Time is = "
<< asctime(ti)
<< endl ;
char currenttime[100] ;
memset(currenttime, '\0', 100) ;
strcpy(currenttime, asctime(ti)) ;
printf("Show the current time by characters array:\n") ;
cout << currenttime << endl ;
string currentTime ;
currentTime.append(asctime(ti));
printf("Show the current time by string:\n") ;
cout << currentTime << endl ;
return 0;
}
运行结果:
Current Day, Date and Time is = Wed Mar 27 11:00:25 2024
Show the current time by characters array:
Wed Mar 27 11:00:25 2024
Show the current time by string:
Wed Mar 27 11:00:25 2024
Process finished with exit code 0