Obtain the current system time (three methods)

Source: Internet
Author: User

# Include <time. h>
# Include <stdio. h>

Void main ()
{/* Method 1
Time_t curTime = time (NULL );
Char * curDate = ctime (& curTime );
Printf (curDate );

-----------------------
| Pass the NULL parameter to time () to obtain the current date and time
| Convert the obtained time and date to the C string type through ctime ()
| Output current time and date

*/

/* Method 2:
| Returns the current system date and time through time,
| Use localtime () to write the current date and time to the struct tm struct.
| Struct tm
| {
| Int tm_sec; // seconds after the minute-[0, 59]
| Int tm_min; // minutes after the hour-[0, 59]
| Int tm_hour; // hours since midnight-[0, 23]
| Int tm_mday; // day of the month-[1, 31]
| Int tm_mon; // months since January-[0, 11]
| Int tm_year; // years since 1900.
| Int tm_wday; // days since Sunday-[0, 6]
| Int tm_yday; // days since January 1-[0,365]
| Int tm_isdst; // daylight savings time flag
| };
| Specify the output format as year, month, day, hour, minute, and second
| Output the formatted date and time to the character array.

Time_t curTime = time (NULL );
Struct tm * localTime = localtime (& curTime );
Char s [50];
Char * format = "% y-% m-% d % H: % M: % S ";
Size_t max_size = 49;
Size_t result = strftime (s, max_size, format, localTime );
If (result! = 0)
Printf (s );

*/

/* Method 3
| Get the current system time through time ()
| Converts the system time to the local time
| Converts the local time to a string (using strftime () internally)
| The current system time and date represented by the output string
Time_t curTime = time (NULL );
Struct tm * localTime = localtime (& curTime );
Char * cTime = asctime (localTime );
Printf (cTime );
*/

Getchar ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.