时间格式的互相转换

#include "stdafx.h"
#include <afxdisp.h>     //使用COleDateTime   
#include "stdio.h"
#include "iostream.h"
#include <string.h>
#include <time.h>
#include <stdio.h>
long GetLongTimeByStrInMfc(char* strTime)
{
      if(strTime == NULL)
          return 0;
      COleDateTime dtTime;
      dtTime.ParseDateTime(strTime);
      SYSTEMTIME st;
     dtTime.GetAsSystemTime(st);
     CTime tTime(st);
     return tTime.GetTime();
}
long GetLongTimeByStrInC(char* strTime)
{
    if(strTime == NULL)
  return 0;
 char years[5] ={""}, month[3] = {""}, day[3] = {""}, /
   hour[3] = {""}, minute[3] = {""}, second[3] = {""};
 strncpy(years,strTime,4);
 strncpy(month,strTime+5,2);
 strncpy(day,strTime+8,2);
 strncpy(hour,strTime+11,2);
 strncpy(minute,strTime+14,2);
 strncpy(second,strTime+17,2);
 struct tm   newTime;
 newTime.tm_year = atoi(years) - 1900;
 newTime.tm_mon  = atoi(month) - 1;
 newTime.tm_mday = atoi(day);
 newTime.tm_hour = atoi(hour);
 newTime.tm_min  = atoi(minute);
 newTime.tm_sec  = atoi(second);
 return mktime(&newTime);
}
int main( void )
{
    struct tm   *newTime;
 //得到当前时间
    time_t      szClock =  time(NULL);
 //把当前时间转为tm结构体,从而可以得到年月日
    newTime = localtime( &szClock );
 char info[MAX_PATH] = {""};
 char curTime[50] = {""};
 //把当前时间组合成一个字符串:yyyy-MM-dd hh:mm:ss
 sprintf(curTime,"%04d-%02d-%02d %02d:%02d:%02d",newTime->tm_year + 1900,newTime->tm_mon+1,newTime->tm_mday, /
  newTime->tm_hour,newTime->tm_min,newTime->tm_sec);
 sprintf(info,"当前时间: time_t: %d , %s  /r/n",szClock ,curTime);
    printf(info);
 //把组合成的yyyy-MM-dd hh:mm:ss字符串通过mfc函数转为time_t时间
    long lcurtime = GetLongTimeByStrInMfc(curTime);
 printf("通过mfc函数把当前时间: %s 转为:time_t: %d /r/n",curTime, lcurtime);
 //把组合成的yyyy-MM-dd hh:mm:ss字符串通过mfc函数转为time_t时间
 lcurtime = GetLongTimeByStrInC(curTime);
 printf("通过c函数把当前时间: %s 转为:time_t: %d /r/n",curTime,lcurtime);
 return 1;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值