在 C++ 中将字符串转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/308390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a string to a date in C++
提问by Ubervan
I know this may be simple but being C++ I doubt it will be. How do I convert a string in the form 01/01/2008 to a date so I can manipulate it? I am happy to break the string into the day month year constituents. Also happy if solution is Windows only.
我知道这可能很简单,但作为 C++,我怀疑它会如此。如何将 01/01/2008 形式的字符串转换为日期以便我可以操作它?我很高兴将字符串分解为日月年组成部分。如果解决方案仅适用于 Windows,也很高兴。
回答by kenny
#include <time.h>
char *strptime(const char *buf, const char *format, struct tm *tm);
回答by Ubervan
I figured it out without using strptime
.
我不使用strptime
.
Break the date down into its components i.e. day, month, year, then:
将日期分解为其组成部分,即日、月、年,然后:
struct tm tm;
time_t rawtime;
time ( &rawtime );
tm = *localtime ( &rawtime );
tm.tm_year = year - 1900;
tm.tm_mon = month - 1;
tm.tm_mday = day;
mktime(&tm);
tm
can now be converted to a time_t
and be manipulated.
tm
现在可以转换为 atime_t
并进行操作。
回答by Ferruccio
You could try Boost.Date_Time Input/Output.
回答by DoubleYou
For everybody who is looking for strptime()
for Windows, it requires the source of the function itself to work. The latest NetBSD code does not port to Windows easily, unfortunately.
对于正在寻找strptime()
Windows 的每个人来说,它需要函数本身的源代码才能工作。不幸的是,最新的 NetBSD 代码不能轻易移植到 Windows。
I myself have used the implementation here(strptime.h and strptime.c).
我自己在这里使用了实现(strptime.h 和 strptime.c)。
Another helpful piece of code can be found here. This comes originally from Google Codesearch, which does not exist anymore.
可以在此处找到另一段有用的代码。这最初来自 Google Codesearch,现在已经不存在了。
Hope this saves a lot of searching, as it took me quite a while to find this (and most often ended up at this question).
希望这可以节省大量搜索,因为我花了很长时间才找到它(并且通常以这个问题结束)。
回答by Sergey Bromirskiy
#include <time.h>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
int main ()
{
time_t rawtime;
struct tm * timeinfo;
int year, month ,day;
char str[256];
cout << "Inter date: " << endl;
cin.getline(str,sizeof(str));
replace( str, str+strlen(str), '/', ' ' );
istringstream( str ) >> day >> month >> year;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;
mktime ( timeinfo );
strftime ( str, sizeof(str), "%A", timeinfo );
cout << str << endl;
system("pause");
return 0;
}
回答by Johan Engblom
Why not go for a simpler solution using boost
为什么不使用 boost 寻求更简单的解决方案
using namespace boost::gregorian;
using namespace boost::posix_time;
ptime pt = time_from_string("20150917");
回答by Adrian Kosmaczewski
The POCO library has a DateTimeParser class which might help you with that. http://www.appinf.com/docs/poco/Poco.DateTimeParser.html
POCO 库有一个 DateTimeParser 类,它可能会帮助你。 http://www.appinf.com/docs/poco/Poco.DateTimeParser.html
回答by Vinoj John Hosan
You can utilize the boost library(cross platform)
您可以使用 boost 库(跨平台)
#include <stdio.h>
#include "boost/date_time/posix_time/posix_time.hpp"
int main()
{
std::string strTime = "2007-04-11 06:18:29.000";
std::tm tmTime = boost::posix_time::to_tm(boost::posix_time::time_from_string(strTime));
return 0;
}
But the format should be as mentioned :)
但格式应该如上所述:)