时间格式的互相转换
2007-08-03 11:25
288 查看
#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;
}
#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;
}
相关文章推荐
- iOS 获取系统时间以及时间与日期格式互相转换
- JS 格林威治时间格式(GMT)与普通时间格式的互相转换
- JS 格林威治时间格式(GMT)与普通时间格式的互相转换
- MySql 格式化时间(包括正常时间格式与unix时间戳的互相转换)
- js时间戳、日期格式 互相转换
- SQL SERVER-时间戳(timestamp)与时间格式(datetime)互相转换
- Excel 时间戳和时间格式的互相转换
- Java时间戳与日期格式直接的互相转换(1:将系统日期格式转为时间戳。2:将指定日期转为时间戳。3:将系统时间戳转为日期格式。4将指定的时间戳转为日期格式)
- js时间格式之间的转换和时间之间的计算
- java字符串怎么转换成时间date格式,并把date再转换成毫秒
- 转:SQL 日期时间格式转换
- javaScript将字符串格式的时间转换为date格式
- Java将Unix时间戳转换成指定格式日期
- png,或jpg等等图片格式和webp图片格式互相转换的在线工具
- SQL Server日期时间格式转换字符串详解
- 秒数转换为视频时间格式
- 将日期时间转换为指定格式
- js 转换 Fri Sep 16 00:00:00 UTC+0800 2011 格式时间函数
- JS Date时间格式的转换
- 时间格式转换工具