VC Get current Time

Source: Internet
Author: User

1. Using the CTime class

CString str;

Get system time

CTime TM;

Tm=ctime:: Getcurrenttime_r ();

Str=tm. Format ("Now time is%y year%m month%d day%x");

MessageBox (STR,NULL,MB_OK);


2: Get system time date (using Getlocaltime)

SYSTEMTIME St;

CString Strdate,strtime;

Getlocaltime_r (&ST);

Strdate.format ("%4d-%2d-%2d", st.wyear,st.wmonth,st.wday);

Strtime.format ("%2d:%2d:%2d", St.whour,st.wminute,st.wsecond);


3. Using GetTickCount

Get program Run time

Long t1= gettickcount_r ();//Get System Run time (MS) before program section starts

Long t1= GetTickCount ();

Sleep (500);

Long t2= Gettickcount_r ();();//Get System Run time (MS) after the end of the program segment

Long t2= GetTickCount ();

Str. Format ("Time:%dms", t2-t1);//The difference between the front and back is the program run time

AfxMessageBox (str);

4. Get the system run time

Long t= gettickcount_r ();

CString str,str1;

Str1. Format ("System is running%d", t/3600000);

STR=STR1;

t%=3600000;

Str1. Format ("%d", t/60000);

STR+=STR1;

t%=60000;

Str1. Format ("%d Seconds", t/1000);

STR+=STR1;

AfxMessageBox (str);


5. Calculate the number of seconds from January 1, 1970 0:0 0 seconds to that point in time

#include <iostream>

#include <ctime>

using namespace Std;

int main () {

time_t Now_time;

Now_time = time (NULL);

cout<<now_time;

return 0;

}


6. Use the system function to change the computer's time setting

#include <stdlib.h>

#include <iostream>

using namespace Std;

void Main () {

System ("Time");

}

VC Get current Time

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.