获取的日期和时间在C [英] getting date and time in c

查看:102
本文介绍了获取的日期和时间在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到的日期作为一个字符串和当前时间作为另一个C.我看了一下time.h中,看到它让我得到了整个事情。难道我那么就得使用一些字符串的方法呢?我注意到,C没有子串的方法? STRCPY好像它可能工作,但我需要一个char *不是一个数组。

I need to get the date as one string and the current time as another in C. I've had a look at time.h and see it allows me to get the whole thing. Do I then have to use some string methods? I notice that C doesn't have a substring method? Strcpy seems like it might work but I need a char * not an array.

任何意见?

感谢

推荐答案

您可以使用的strftime 此:

struct tm *tm;
time_t t;
char str_time[100];
char str_date[100];

t = time(NULL);
tm = localtime(&t);

strftime(str_time, sizeof(str_time), "%H %M %S", tm);
strftime(str_date, sizeof(str_date), "%d %m %Y", tm);

这篇关于获取的日期和时间在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆