C或C++怎么获取xxxx-xx-xx格式的日期

sinat_24499499 2014-12-14 02:03:08
RT,求问各位大神,烦了好长时间了
...全文
655 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-12-15
  • 打赏
  • 举报
回复
仅供参考
#include <stdio.h>
#include <string.h>
#include <time.h>
struct tm st;
time_t tt;
char timstr[40];
char tmpbuf[128];
char c;
int t;
int AP() {
    if ('P'==c || 'p'==c) {st.tm_hour+=12;return 1;}
    if ('A'==c || 'a'==c)                 return 1;
                                          return 0;
}
int dtparse() {
    if (7==sscanf(timstr,"%d/%d/%4d %d:%d:%d %c",&st.tm_mday,&st.tm_mon ,&st.tm_year,&st.tm_hour,&st.tm_min,&st.tm_sec,&c)) if (AP()) goto STEP2;
    if (6==sscanf(timstr,"%d/%d/%4d %d:%d%c"    ,&st.tm_mday,&st.tm_mon ,&st.tm_year,&st.tm_hour,&st.tm_min           ,&c)) if (AP()) goto STEP1;
    if (6==sscanf(timstr,"%4d/%d/%d %d:%d %c"   ,&st.tm_year,&st.tm_mon ,&st.tm_mday,&st.tm_hour,&st.tm_min           ,&c)) if (AP()) goto STEP1;
    if (5==sscanf(timstr,"%d.%d.%4d %d:%d"      ,&st.tm_mon ,&st.tm_mday,&st.tm_year,&st.tm_hour,&st.tm_min              ))           goto STEP1;
    if (6==sscanf(timstr,"%4d-%d-%d %d:%d:%d"   ,&st.tm_year,&st.tm_mon ,&st.tm_mday,&st.tm_hour,&st.tm_min,&st.tm_sec   ))           goto STEP2;
STEP1:
    st.tm_sec=0;
STEP2:
    st.tm_year-=1900;
    if (st.tm_mon>12) {t=st.tm_mon;st.tm_mon=st.tm_mday;st.tm_mday=t;}
    st.tm_mon-=1;
    tt=mktime(&st);
    if (-1!=tt) {
        strftime(tmpbuf,128,"%Y-%m-%d %H:%M:%S",localtime(&tt));
        return 1;
    } else {
        printf("[%s] is Invalid time string!\n",timstr);
        return 0;
    }
}
int main() {
    strcpy(timstr,"10.01.2012 15:43"        );if (dtparse()) printf("[%s] is [%s]\n",timstr,tmpbuf);
    strcpy(timstr,"8/11/2006 11:10AM"       );if (dtparse()) printf("[%s] is [%s]\n",timstr,tmpbuf);
    strcpy(timstr,"2006/10/31 04:50 PM"     );if (dtparse()) printf("[%s] is [%s]\n",timstr,tmpbuf);
    strcpy(timstr,"1/6/2010 5:20:12 PM"     );if (dtparse()) printf("[%s] is [%s]\n",timstr,tmpbuf);
    strcpy(timstr,"13/12/2012 11:16:17 a.m.");if (dtparse()) printf("[%s] is [%s]\n",timstr,tmpbuf);
    strcpy(timstr,"2013-06-08 09:56:59"     );if (dtparse()) printf("[%s] is [%s]\n",timstr,tmpbuf);
    return 0;
}
//[10.01.2012 15:43] is [2012-10-01 15:43:00]
//[8/11/2006 11:10AM] is [2006-11-08 11:10:00]
//[2006/10/31 04:50 PM] is [2006-10-31 16:50:00]
//[1/6/2010 5:20:12 PM] is [2010-06-01 17:20:12]
//[13/12/2012 11:16:17 a.m.] is [2012-12-13 11:16:17]
//[2013-06-08 09:56:59] is [2013-06-08 09:56:59]
//
sinat_24499499 2014-12-14
  • 打赏
  • 举报
回复
引用 4 楼 ri_aje 的回复:
[quote=引用 2 楼 sinat_24499499 的回复:] [quote=引用 1 楼 ri_aje 的回复:] 获取时间以后自己格式化吧。
怎么格式化,能举个例子吗?[/quote] http://www.cplusplus.com/reference/ctime/strftime/[/quote] 多谢
ri_aje 2014-12-14
  • 打赏
  • 举报
回复
引用 2 楼 sinat_24499499 的回复:
[quote=引用 1 楼 ri_aje 的回复:] 获取时间以后自己格式化吧。
怎么格式化,能举个例子吗?[/quote] http://www.cplusplus.com/reference/ctime/strftime/
ysnis 2014-12-14
  • 打赏
  • 举报
回复
C语言中time_t数据
sinat_24499499 2014-12-14
  • 打赏
  • 举报
回复
引用 1 楼 ri_aje 的回复:
获取时间以后自己格式化吧。
怎么格式化,能举个例子吗?
ri_aje 2014-12-14
  • 打赏
  • 举报
回复
获取时间以后自己格式化吧。

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧