boost - 日期时间 - gregorian

本文介绍如何使用Boost库进行日期和时间的操作,包括日期的获取、初始化、格式转换及日期计算等。通过实例演示了不同日期格式之间的转换方法,并展示了如何进行日期加减运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

头文件
boost/date_time/gregorian/gregorian.hpp

date类

//获取今天的如期
date today = day_clock::local_day();

//日期初始化
date d1(2018, 9, 12);
date d2 = from_string("2018-09-12");
date d3 = from_string("2018/09/12");
date d4 = from_undelimited_string("20180912");

//month,week返回的是英文字符串,需要用as_number获取数字
cout << d1.year() << "年" << d1.month().as_number() << "月" << d1.day() << "日" << endl;//2018年9月12日
cout << to_simple_string(d2) << endl;//2018-Sep-12
cout << to_iso_string(d3) << endl;//20180912
cout << to_iso_extended_string(d4) << endl;//2018-09-12

cout << today.year() << endl;//2018
cout << today.month() << endl;//Sep
cout << today.day() << endl;//12
cout << today.week_number() << endl;//37
cout << today.day_of_week() << endl;//Wed
cout << today.day_of_year() << endl;//255
cout << today.end_of_month() << endl;//2018-Sep-30

日期计算

days ds1(5);
days ds2(-2);
date_duration dd = ds1 + ds2;
cout << dd << endl;//3
cout << dd.days() << endl;//3

weeks ws(1);
cout << ws.days() << endl;//7
cout << dd + ws << endl;//10

months ms(1);
cout << d1 + ms << endl;//2018-Oct-12

years ys(1);
cout << d1 + ys << endl;//2019-Sep-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值