笔记:时间戳和日期相互转换
来源:互联网 发布:使命召唤 知乎 编辑:程序博客网 时间:2024/05/16 16:01
// tttt.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void nowTime();//显示当前时间
void nowTime2();
void enterTime(int a);//固定时间戳转换日期格式
long getTick(char *str_time);//转换成时间戳
long getTick2(char *str_time);//转换成时间戳 方法2
//方法2还是好用一点 ,方法1有瑕疵
int main(int argc, char* argv[])
{
char str_time[19];
printf("请输入时间:");
gets(str_time);
// printf("%ld",getTick(str_time));
enterTime(getTick2(str_time));
nowTime();
return 0;
}
long getTick(char *str_time)
{
struct tm stm;
int iY, iM, iD, iH, iMin, iS;
memset(&stm,0,sizeof(stm));
//将字符串转换为整形,遇到非整形停止
iY = atoi(str_time);
iM = atoi(str_time+5);//从第5个数开始
iD = atoi(str_time+8);//从第8个数开始
iH = atoi(str_time+11);
iMin = atoi(str_time+14);
iS = atoi(str_time+17);
stm.tm_year=iY-1900;
stm.tm_mon=iM-1;
stm.tm_mday=iD;
stm.tm_hour=iH;
stm.tm_min=iMin;
stm.tm_sec=iS;
return mktime(&stm);
}
long getTick2(char *str_time)
{
struct tm stm;
// time_t t;
memset(&stm,0,sizeof(stm));//结构体赋0值
sscanf(str_time,"%d-%d-%d %d:%d",&stm.tm_year,&stm.tm_mon,&stm.tm_mday,&stm.tm_hour,&stm.tm_min);
stm.tm_year-=1900;
stm.tm_mon-=1;
// t=mktime(&stm);
return mktime(&stm);
}
void enterTime(int a)
{
time_t t=a;
struct tm *lt;//tm结构体
lt=localtime(&t);//将时间戳转换为本地时间
char nowtime[24];
memset(nowtime,0,sizeof(nowtime));//将数组中24个数替换为0
strftime(nowtime,24,"%Y-%m-%d %H:%M",lt);
printf("\nnowtime:%s",nowtime);
}
void nowTime2()
{
time_t t;
struct tm * lt;
time (&t);//获取Unix时间戳。
lt = localtime (&t);//转为时间结构。
printf ( "%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);//输出结果
}
void nowTime()
{
time_t t=time(NULL);
struct tm *lt;
int ii=time(&t);//获取时间戳
lt=localtime(&t);
char nowtime[24];
memset(nowtime,0,sizeof(nowtime));//将数组中24个数替换为0
strftime(nowtime,24,"%Y-%m-%d %H:%M:%S",lt);
printf("\nnowtime:%s",nowtime);
}
/*
#include <time.h>
size_t strftime(char *str, size_t count, const char *format, const struct tm *tm);
str, 表示返回的时间字符串
count, 要写入的字节的最大数量
format, 格式字符串由零个或多个转换符和普通字符(除%)
tm, 输入时间
*/
//
#include "stdafx.h"
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void nowTime();//显示当前时间
void nowTime2();
void enterTime(int a);//固定时间戳转换日期格式
long getTick(char *str_time);//转换成时间戳
long getTick2(char *str_time);//转换成时间戳 方法2
//方法2还是好用一点 ,方法1有瑕疵
int main(int argc, char* argv[])
{
char str_time[19];
printf("请输入时间:");
gets(str_time);
// printf("%ld",getTick(str_time));
enterTime(getTick2(str_time));
nowTime();
return 0;
}
long getTick(char *str_time)
{
struct tm stm;
int iY, iM, iD, iH, iMin, iS;
memset(&stm,0,sizeof(stm));
//将字符串转换为整形,遇到非整形停止
iY = atoi(str_time);
iM = atoi(str_time+5);//从第5个数开始
iD = atoi(str_time+8);//从第8个数开始
iH = atoi(str_time+11);
iMin = atoi(str_time+14);
iS = atoi(str_time+17);
stm.tm_year=iY-1900;
stm.tm_mon=iM-1;
stm.tm_mday=iD;
stm.tm_hour=iH;
stm.tm_min=iMin;
stm.tm_sec=iS;
return mktime(&stm);
}
long getTick2(char *str_time)
{
struct tm stm;
// time_t t;
memset(&stm,0,sizeof(stm));//结构体赋0值
sscanf(str_time,"%d-%d-%d %d:%d",&stm.tm_year,&stm.tm_mon,&stm.tm_mday,&stm.tm_hour,&stm.tm_min);
stm.tm_year-=1900;
stm.tm_mon-=1;
// t=mktime(&stm);
return mktime(&stm);
}
void enterTime(int a)
{
time_t t=a;
struct tm *lt;//tm结构体
lt=localtime(&t);//将时间戳转换为本地时间
char nowtime[24];
memset(nowtime,0,sizeof(nowtime));//将数组中24个数替换为0
strftime(nowtime,24,"%Y-%m-%d %H:%M",lt);
printf("\nnowtime:%s",nowtime);
}
void nowTime2()
{
time_t t;
struct tm * lt;
time (&t);//获取Unix时间戳。
lt = localtime (&t);//转为时间结构。
printf ( "%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);//输出结果
}
void nowTime()
{
time_t t=time(NULL);
struct tm *lt;
int ii=time(&t);//获取时间戳
lt=localtime(&t);
char nowtime[24];
memset(nowtime,0,sizeof(nowtime));//将数组中24个数替换为0
strftime(nowtime,24,"%Y-%m-%d %H:%M:%S",lt);
printf("\nnowtime:%s",nowtime);
}
/*
#include <time.h>
size_t strftime(char *str, size_t count, const char *format, const struct tm *tm);
str, 表示返回的时间字符串
count, 要写入的字节的最大数量
format, 格式字符串由零个或多个转换符和普通字符(除%)
tm, 输入时间
*/
阅读全文
0 0
- 笔记:时间戳和日期相互转换
- 时间戳和日期相互转换
- Python 时间戳和日期相互转换
- SQL 日期 和 时间戳 相互转换
- PHP时间戳和日期相互转换
- jquery时间戳和日期相互转换
- 时间戳日期相互转换
- PHP+Mysql日期时间和UNIX时间戳相互转换
- 时间戳和普通日期的相互转换
- 不同语言对时间戳和日期的相互转换
- js 时间戳和日期字符串相互转换
- java 时间戳和日期的相互转换
- Python时间戳和日期的相互转换
- Go时间戳和日期字符串的相互转换
- javascript时间戳和日期字符串相互转换
- js时间戳和日期字符串相互转换
- javascript时间戳和日期字符串相互转换
- Python时间戳和日期的相互转换
- ztree:数据字典树,右键菜单
- [PTA] 7-23 币值转换(20 分)
- Ubuntu 安装 MongoDB 详细过程
- 二维码扫描控件
- kafka开发笔记
- 笔记:时间戳和日期相互转换
- 笔记:css3 transition 实现div宽度以中心为原点向两边伸长
- avformat_open_input函数分析
- WOJ1008-Feeding Animals(II)
- HDU1520 Anniversary party —— 树形DP
- 微信公众平台 多媒体文件上传接口调试工具
- node-sass安装出错问题解决
- String intern
- POJ 3469 Dual Core CPU (最小割)