如何在C中打印日期? [英] How do I printf a date in C?

查看:130
本文介绍了如何在C中打印日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从类似 01/01/01的字符串中打印日期,并得到类似 2001年1月1日,星期一的信息。

I'm trying to print a date from a string like "01/01/01" and get something like "Monday First January 2001.

我发现了一些东西

有什么帮助吗?

谢谢,

推荐答案

您可以使用 strptime 将字符串日期转换为 struct tm

You can use strptime to convert your string date to struct tm

struct tm tm;
strptime("01/26/12", "%m/%d/%y", &tm);

然后使用 strftime

char str_date[256];
strftime(str_date, sizeof(str_date), "%A, %d %B %Y", &tm);
printf("%s\n", str_date);

这篇关于如何在C中打印日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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