// Solution-advantage: only the C standard library is used; disadvantage: only seconds are allowed
# Include <time. h>
# Include <stdio. h>
Int main (void)
{
Time_t t = time (0 );
Char TMP [64];
Strftime (TMP, sizeof (TMP), "% Y/% m/% d % x % A % j day % Z this year", localtime (& T ));
Puts (TMP );
Return 0;
}
Size_t strftime (char * strdest, size_t maxsize, const char * format, const struct TM * timeptr );
Generate a string based on the format string.
Struct TM * localtime (const time_t * timer );
Obtain the local time. the result obtained by localtime is returned by the structure TM.
The returned string can be in the following format:
% A the abbreviation of the day of the week. Eg: TUE
% A full name of the day of the week. Eg: Tuesday
The abbreviation of % B month name.
% B full name of the month name.
% C the local date time is better than the string.
% D indicates the day of the month (range: 00 to 31) with a number ). Date
% H indicates the hour in the 24-hour format (ranging from 00 to 23 ).
% I represents the hour in 12-hour format (range: 01 to 12 ).
% J indicates the day of the year (range: 001 to 366 ).
The number of % m months (ranging from 1 to 12 ).
% M minutes.
% P uses ''am'' or ''pm ''to indicate the local time.
% S seconds.
% U indicates the week of the current year. The first week starts from the first Sunday.
% W indicates the week of the current year. The first week starts from the first Monday.
% W indicates the day of the week by number (0 indicates Sunday ).
% X date representation without time.
% X does not include the time representation of the date. Eg: 15:26:30
The two digits % Y indicate the year (range: 00 to 99 ).
% Y indicates the complete year number, that is, the four-digit number. Example: 2008
% Z (% Z) Time Zone or abbreviation. Eg: China Standard Time
% Characters.
// Solution 2 advantages: accurate to milliseconds; disadvantage: Windows API is used
# Include <windows. h>
# Include <stdio. h>
Int main (void)
{
Systemtime sys;
Getlocaltime (& Sys );
Printf ("% 4D/% 02d/% 02d % 02d: % 02d: % 02d. % 03d week % 1D \ n ", sys. wyear, sys. wmonth, sys. wday, sys. whour, sys. wminute, sys. wsecond, sys. wmilliseconds, sys. wdayofweek );
Return 0;
}
// Solution 3. Advantages: using system functions, you can modify the system time.
// This file must be a C ++ File
# Include <stdlib. h>
# Include <iostream>
Using namespace STD;
Void main ()
{
System ("time ");
}
// Solution 4: Convert the current time to seconds, and then use the corresponding time to convert
// This file must be a C ++ File
# Include <iostream>
# Include <ctime>
Using namespace STD;
Int main ()
{
Time_t now_time;
Now_time = Time (null );
Cout <now_time;
Return 0;
}
0 0 0
(Please comment on the article)