如何把本地时间转成固定格式字符串?
如何把本地时间转换成字符串我已经知道怎么做了,现在的问题是如何转成固定长度的字符串
如:需要的到的字符串是20120904090209,就是如何把一位数,前面加个0,成两位数。
我用SYSTEMTIME getLocalTime()取得本地时间,用itoa转换车字符串,用strcpy()、strcat()整合到一个字符数组中。谁有更好的方法也可以交流一下。
[解决办法]
这个可以吗??
- C/C++ code
#include <stdio.h>#include <iostream>#include <string>#include <Windows.h>#include <WinBase.h>using namespace std;string GetTime(){ SYSTEMTIME t; GetLocalTime(&t); char str[20]; sprintf( str, "%4d%02d%02d%02d%02d%02d",t.wYear,t.wMonth,t.wDay,t.wHour,t.wMinute,t.wSecond); return string(str);}int main(){ string time = GetTime(); cout << time<<endl; return 0;}