CTime、time_t、COleDateTime、unsigned long、CString互转

1.unsigned long->CTime
  unsigned long nTime;   //nTime类型为unsigned long
  CTime objTime(nTime);   //CTime类型变量直接获取时间值
  string strTime;    //格式化string变量
  strTime.Format("d-d-d d:d:d", objTime.GetYear(), objTime.GetMonth(), objTime.GetDay(),
  objTime.GetHour(), objTime.GetMinute(), objTime.GetSecond());
  printf("%s\n", strTime);


2.CTime -> unsigned long
  CTime objTime;     //定义CTime类型变量
  m_DateTimeCtrlTime.GetTime(objTime);  //在MFC界面获取时间值(Date Time Picker)

  unsigned long nTime = objTime.GetTime(); //unsigned long类型变量获取时间长度

==================================================================

    CString strDate;
    COleDateTime ole_time;
    CTime c_time;
3.COleDateTime->CTime
  SYSTEMTIME sys_time;
  ole_time.GetAsSystemtime(sys_time);
  c_time = CTime(sys_time);
4.CTime->COleDateTime
  SYSTEMTIME sys_time;
  c_time.GetAsSystemTime(sys_time);
  ole_time = COleDateTime(sys_time);
5.CString->COleDateTime
  strDate = "2009-4-25 12:30:29";
  ole_time.ParseDateTime(strDate);
6.COleDateTime->CString
  strDate = ole_time.Format("%Y-%m-%d %H:%M:%S");
7.CTime->CString

  strDate = c_time.Format("%Y-%m-%d %H:%M:%S");

================================================================

8.unsigned long->time_t
  unsigned long nTime;   //nTime初始类型为unsigned long
  ...     //nTime赋值
  time_t tTime = (time_t)nTime;  //将nTime(32位)强制转换为time_t类型,或者INT64类型(64位)
  struct tm *tmTime;   //定义tm类型指针
  tmTime = localtime(&tTime);  //获取时间
  printf("%s\n", asctime(tmTime));  //将时间表示为英文语句形式,如:Mon Dec 28 12:33:50 1998

9. time_t -> unsigned long
  CString strTime;   //CString类型
  ...     //在MFC界面获取时间值
  struct tm stTime;   //定义tm类型变量
  sscanf((LPSTR)(LPCTSTR)strTime, "d-d-d d:d:d",
   &stTime.tm_year, &stTime.tm_mon, &stTime.tm_mday, &stTime.tm_hour,
  &stTime.tm_min, &stTime.tm_sec); //强制转换为char*,再使tm变量获取时间值
  stTime.tm_year = stTime.tm_year - 1900;
  stTime.tm_mon = stTime.tm_mon - 1; //转换相对1900.01的时间长度
  unsigned long nTime = (long)_mkgmtime(&stTime);  //转换成了unsigned long类型


你可能感兴趣的:(C++)