gpt4 book ai didi

c - 如何从日期时间获取时间

转载 作者:行者123 更新时间:2023-11-30 14:50:24 26 4
gpt4 key购买 nike

是否有像 ctime 这样的函数可以仅获取 char* 形式的小时、分钟和秒?

我已经尝试过

time_t time
struct tm* currtime = localtime(time);
printf("%d:%d:%d", currtime->tm_hour, currtime->tm_min, currtime->tm_sec);

但对于单位数字值,它将打印 8:2:5 而不是 08:02:05。

最佳答案

尝试以下操作:

`
struct timeval cur_time;
gettimeofday(&cur_time, NULL);
char dateTime[10 + 1] = {0};

strftime(dateTime, 10, "%H%M%S", localtime(&cur_time.tv_sec));
`

关于c - 如何从日期时间获取时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49023356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com