C++ 将字符串转换成date类型的数据
来源:互联网 发布:cad线切割编程软件 编辑:程序博客网 时间:2024/06/08 01:01
#include <time.h> /* time_t, struct tm, time, localtime, strftime */#include <string>#include <iostream>#include <vector>char* asctime(const struct tm *timeptr){ static const char mon_name[][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; static char result[13]; sprintf(result, "%.3s%3d %d\n", mon_name[timeptr->tm_mon], timeptr->tm_mday, 1900 + timeptr->tm_year); return result;}//change string to intint parse_int(const char* str) { int value = atoi(str); return value;}struct tm* stringToDate(const std::string dateString, const std::string splitstr){ std::string datestr = dateString; std::vector<int> yymmdd(3,0); std::size_t frist = datestr.find(splitstr); std::size_t last = datestr.find_last_of(splitstr);/* std::cout <<"year:" << datestr.substr(0, frist) << std::endl; std::cout <<"month:" << datestr.substr(frist + 1, last - frist - 1) << std::endl; std::cout <<"day:"<< datestr.substr(last + 1, datestr.length()) << std::endl; */ time_t rawtime; struct tm * timeinfo; time (&rawtime); timeinfo = localtime (&rawtime); //将上面取得的数转换成int型数据后 存入vector yymmdd中 yymmdd[0] = parse_int(datestr.substr(0, frist).c_str()); yymmdd[1] = parse_int(datestr.substr(frist + 1, last - frist - 1).c_str()); yymmdd[2] = parse_int(datestr.substr(last + 1, datestr.length()).c_str()); timeinfo->tm_mday = yymmdd[2]; timeinfo->tm_mon = yymmdd[1] - 1; timeinfo->tm_year = yymmdd[0] -1900; return timeinfo;}int main (){ std::string mystr = "2009-08-15"; printf ("You input date is: %s", asctime(stringToDate(mystr,"-"))); return 0;}
0 0
- C++ 将字符串转换成date类型的数据
- 将Date日期类型的字段转换成json字符串
- 将long类型的字符串转换成date
- 将date类型的数据转换成XMLGregorianCalendar
- mysql将timestamp类型转换成date类型数据
- 将String类型转换成Date类型
- 关于java将String类型的时间字符串转换为Date类型的问题
- 在java中怎样将Date类型转换成字符串类型
- 将java.util.Date类型转换成json时,使用JsonValueProcessor将date转换成希望的类型
- 关于如何将含有Date类型属性的对象转换成符合实际应用的Json字符串
- 将string转换成date类型
- 如何将String转换成Date类型
- JS 将Date对象和字符串转换成日期类型
- 将Date类型的数据存入数据库
- JS将字符串类型的时间格式转换为date格式
- javascript中将字符串转换成date类型
- JS把字符串转换成Date类型
- 将字符串数据转换为其他类型
- linux下i2c通用接口读取和处理mag3110地磁传感器程序
- Bug 10121589 ORA-600 [kjbmprlst:shadow]
- tar命令详解
- eclipse中ADT离线安装方法
- (前、中、后)序遍历二叉树的递归、非递归算法!
- C++ 将字符串转换成date类型的数据
- SymmetricTree
- GNU对C语言的扩展---结构体
- 珍惜着美好的时光
- [Java][MyBatis]mapperLocations属性通配符的使用
- 在iOS开发中自动获取当前的位置(GPS定位)
- 用户配置文件状态变成“备份”
- C++ 关于断点续传的方法,使用CURL技术
- HDOJ_就这么个烂题总是WA先放这把