C + + Get system Current time

Source: Internet
Author: User
Tags current time local time month name printf range

#include

* Method One

time_t TT = time (NULL);/This sentence returns just a moment CuO

tm* t= localtime (&TT);

printf ("%d-%02d-%02d%02d:%02d:%02dn",

T->tm_year + 1900,

T->tm_mon + 1,

T->tm_mday,

T->tm_hour,

T->tm_min,

T->TM_SEC);

* Method II

SYSTEMTIME st = {0};

Getlocaltime (&ST);

printf ("%d-%02d-%02d%02d:%02d:%02dn",

St.wyear,

St.wmonth,

St.wday,

St.whour,

St.wminute,

St.wsecond);

The following several, is the online search: Reprint address: http://apps.hi.baidu.com/share/detail/17815869

Personally feel that the second is more practical, but also the most commonly used ~

However, when the calculation algorithm time is time-consuming, do not forget second, can not only use milliseconds to reduce, otherwise negative, if the algorithm takes too long to use the minutes. Otherwise, hours ...

Scenario-Benefits: Only use C standard library; Disadvantage: only accurate to second level

#include

#include

int main (void)

{

time_t t = time (0);

Char tmp[64];

Strftime (TMP, sizeof (TMP), "%y/%m/%d%x%A%j Days of the Year",%z (localtime));

Puts (TMP);

return 0;

}

size_t strftime (char *strdest, size_t maxsize, const char *format, const struct TM *timeptr);

Generates a string from a format string.

struct TM *localtime (const time_t *timer);

Obtain local time, the results obtained by the localtime are returned by the structure TM

The returned string can be determined in the following format:

%a the abbreviation of the week. Eg:tue

%A the full name of the week. Eg:tuesday

The abbreviation for the%b month name.

The full name of the%B month name.

%c Local End Date time is a better representation string.

%d numbers indicate the day of the month (range 00 to 31). Date

%H uses a 24-hour system number to indicate the number of hours (range 00 to 23).

%I A 12-hour number is used to indicate the number of hours (ranging from 01 to 12).

%j the number of days of the year (ranging from 001 to 366).

Number of%m months (ranging from 1 to 12).

%m minutes.

%p indicates the local end time with ' AM ' or ' PM '.

%s Number of seconds.

The%u number is expressed as the week ordinal of the year, and the first one week begins in Sunday.

The%w number is expressed as the week ordinal of the year, and the first one week begins in Monday.

%w the number of days of the week (0 for Sunday).

%x does not contain a date representation of time.

%x does not contain a time representation of dates. Eg:15:26:30

%y two digits indicates the year (range from 00 to 99).

%Y full year digits, that is, four digits. eg:2008

%Z (%Z) time zone or name abbreviation. EG: China Standard Time

%%% character.

Scheme Two advantages: can be accurate to the millisecond; disadvantage: Using Windows API

#include

#include

int main (void)

{

SYSTEMTIME SYS;

Getlocaltime (&sys);

printf ("%4d/%02d/%02d%02d:%02d:%02d.%0 3d Week%1DN ", Sys.wyear,sys.wmonth,sys.wday,sys.whour,sys.wminute, Sys.wsecond,sys.wmilliseconds,sys.wdayofweek);

return 0;

}

Scheme III, advantages: the use of system functions, but also to modify the system time

This file must be a C + + file

#include

#include

using namespace Std;

void Main ()

{

System ("Time");

}

Scenario four, the current time is converted to a second level, and then the corresponding time conversion can be

This file must be a C + + file

#include

#include

using namespace Std;

int main ()

{

time_t Now_time;

Now_time = time (NULL);

cout<

return 0;

}

Note: There is a difference between getlocaltime () and GetSystemTime ().

Related Article

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.