为了快速的时间格式转换. C++ 引进了 get_time 和 put_time 函数
#include <iostream>
#include<iomanip>
#include<ctime>
int main()
{
std::time_t t = std::time(NULL);
std::tm tm = *std::localtime(&t);
std::cout.imbue(std::locale("CHS"));
std::cout <<std::put_time(&tm,"%Y-%m-%d %H:%M:%S") << '\n';
// std::cout.imbue(std::locale("ja_JP.utf8"));
// std::cout << "ja_JP: " << std::put_time(&tm, "%c %Z") << '\n';
system("pause");
}
下面是时间函数格式串的一些特定转义参数: 注::VS2010 不支持C++11的格式 带E前缀的表示在当地语言环境中的表示... 带O前缀的表示使用当地语言环境的计数文字 常用 时:分:秒 %H:%M:%S <==> %T (C++11标准) 当地时间标准 %X 十二小时制 %I (%p <==> a.m|p.m 或者当地写法) 年-月-日 %Y:%m:%d <==> %F (C++11标准) 当期日期标准 %x 一年的第几天 %j 第几周 %U (00-53) 周日为第一天 %W (00-53) 周一为第一天 %V (01-53) 周一为第一天 (C++11标准) 周一为星期的第一天 每年的第一星期为包含1月4号的那个星期. 或者包含第一个星期四那个星期 星期几 %w (0-6) %u(1-7)(C++11标准) 星期几 %a 单词简写 %A 单词全称 (当地写法)
Conversion specifier |
Explanation |
Used fields |
% |
writes literal %. The full conversion specification must be %%. |
|
n(C++11) |
writes newline character |
|
t(C++11) |
writes horizontal tab character |
|
Year |
Y |
writes year as a 4 digit decimal number XXXX 4位数年份 |
tm_year |
EY(C++11) |
writes year in the alternative representation, e.g.平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale |
tm_year |
y |
writes last 2 digits of year as a decimal number ( XX range [00,99]) 两位数年份 |
tm_year |
Oy(C++11) |
writes last 2 digits of year using the alternative numeric system, e.g. 十一 instead of 11 in ja_JP locale |
tm_year |
Ey(C++11) |
writes year as offset from locale's alternative calendar period %EC (locale-dependent) |
tm_year |
C(C++11) |
writes first 2 digits of year as a decimal number (range [00,99]) 公历年的前两位数 |
tm_year |
EC(C++11) |
writes name of the base year (period) in the locale's alternative representation, e.g. 平成 (Heisei era) in ja_JP |
tm_year |
G(C++11) |
writes ISO 8601 week-based year, i.e. the year that contains the specified week. In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:
- Includes January 4
- Includes first Thursday of the year
|
tm_year,tm_wday,tm_yday |
g(C++11) |
writes last 2 digits of ISO 8601 week-based year, i.e. the year that contains the specified week (range[00,99]). In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:
- Includes January 4
- Includes first Thursday of the year
|
tm_year,tm_wday,tm_yday |
Month |
b |
writes abbreviated month name, e.g. Oct (locale dependent) 月份英文简称 |
tm_mon |
h(C++11) |
synonym of b 代名词? |
tm_mon |
B |
writes full month name, e.g. October (locale dependent) 月份英文全称 |
tm_mon |
m |
writes month as a decimal number (range [01,12]) 月份两位数字 |
tm_mon |
Om(C++11) |
writes month using the alternative numeric system, e.g. 十二 instead of 12 in ja_JP locale |
|