VC的时间操作似乎是比较头疼,特别是对一些新手,这里提供了一些时间操作的例子,供大家参考
时间获得:
//获得系统日期
CString GetSysDate()
{
CTime tm = CTime::GetCurrentTime();
int year = tm.GetYear();
int mouth = tm.GetMonth();
int day = tm.GetDay();
CString systime = "";
systime.Format("%d-%d-%d",year,mouth,day);
return systime;
}
//获得系统时间
CString GetSysTime()
{
CTime tm = CTime::GetCurrentTime();
int hour = tm.GetHour();
int min = tm.GetMinute();
int sec = tm.GetSecond();
CString systime = "";
systime.Format("%d:%d:%d",hour,min,sec);
return systime;
}
//获得系统日期包刮时间
CString GetSysTime()
{
CTime tm = CTime::GetCurrentTime();
int year = tm.GetYear();
int mouth = tm.GetMonth();
int day = tm.GetDay();
int hour = tm.GetHour();
int min = tm.GetMinute();
int sec = tm.GetSecond();
CString systime = "";
systime.Format("("%d-%d-%d %d:%d:%d",year,mouth,day,hour,min,sec);
return systime;
}
//获得年份
int GetYear()
{
CTime tm = CTime::GetCurrentTime();
return tm.GetYear();
}
//获得月份
int GetMouth()
{
CTime tm = CTime::GetCurrentTime();
return tm.GetMonth();
}
//获得日
int GetDay()
{
CTime tm = CTime::GetCurrentTime();
return tm.GetDay();
}
//获得时
int GetHour()
{
CTime tm = CTime::GetCurrentTime();
return tm.GetHour();
}
//获得分
int GetMinu()
{
CTime tm = CTime::GetCurrentTime();
return tm.GetMinute();
}
//获得秒
int GetSec()
{
CTime tm = CTime::GetCurrentTime();
return tm.GetSecond();
}
//算出两个时间相差长度
int TimeJianFa(int y1,int m1,int d1,int h1,int ms1,int s1,int y2,int m2,int d2,int h2,int ms2,int s2)
{
CTime m_Time1(y1, m1, d1, h1, ms1, s1 );
CTime m_Time2(y2, m2, d2, h2, ms2, s2 );
UINT t1 = m_Time1.GetTime();
UINT t2 = m_Time2.GetTime();
UINT mDelta = t2 - t1;
int mDay = mDelta;
return mDay;
}