date类的对外接口很像C语言中的tm结构,也可以获取它保存的年、月、日、星期等成分,但date还提供了更多的操作。成员函数year()、month()和day()分别返回日期的年、月、日:
date d(2017,6,1);
assert(d. year()==2017);
assert(d. month()==6);
assert(d. day()==1);
成员函数year_month_day()返回一个date::ymd_type结构,可以一次性地获取年月日数据:
date:: ymd_type ymd=d. year_month_day();
assert(ymd. year ==2017);
assert(ymd. month ==6);
assert(ymd. day ==1);
成员函数day_of_week()返回date的星期数,0表示星期天。day_of_year()返回date是当年的第几天(最多是366)。end_of_month()返回当月的最后一天的date对象:
cout<
cout <
assert(d.end_of_month()==date(2017,6,30));
成员函数week_number()返回date所在的周是当年的第几周,范围是0至53:
cout<
cout <
cout<
date还有五个is_xxx()函数,用于检验日期是否是一个特殊日期,它们是:
is_infinity():是否是一个无限日期;
is_neg_infinity():是否是一个负无限日期;
is_pos_infinity():是否是一个正无限日期;
is_not_a_date():是否是一个无效日期;
is_special():是否是任意一个特殊日期。
它们的用法如下:
assert(date(pos_infin). is_infinity());
assert(date(pos_infin), is_pos_infinity());
assert(date(neg_infin). is neg _infinity());
assert(date(not a date time). is not a date());
assert(date(not a date_time). is _special());
assert(! date(2017,5,31). is_special());
date还提供了除了获取它保存的年、月、日、星期等成分的其它操作,以上就是在C++中,date类访问日期的操作方法。
举报/反馈