原理及代码都很简单(比网上那些用time结构体转来转去方便多了),就不讲了,直接上码:
#include <Windows.h>#include <atlstr.h>#include <ATLComTime.h>#include <atltime.h>int main(){//字符串转时间戳CString strTime = "-07-18 19:30:22";COleDateTime oletime;__int64 instatime = 0;if (oletime.ParseDateTime(strTime)){CTime ctime(oletime.GetYear(), oletime.GetMonth(), oletime.GetDay(), oletime.GetHour(), oletime.GetMinute(), oletime.GetSecond());instatime = ctime.GetTime();printf("时间戳:%ld\n",instatime);}//时间戳转字符串COleDateTime oletime2(instatime);CString strDate = oletime2.Format(L"%Y-%m-%d %H:%M:%S");printf("时间:%S\n", strDate.GetBuffer());system("pause");return 0;}
程序输出: