Skip to content

Datetime class documentation

Jeremy Dumais edited this page Mar 6, 2020 · 1 revision

datetime class

Constructors

datetime(); // Get the current date and time
datetime(int year, 
	int month, 
	int day, 
	int hour = 0, 
	int minute = 0, 
	int second = 0, 
	period day_period = period::undefined);

Available methods

string to_string();
string to_shortdate_string();
int get_year();
int get_month();
int get_day();
int get_hour();
int get_minute();
int get_second();
weekday get_weekday();
void add_years(int nb_years);
void add_months(int nb_months);
void add_days(int nb_days);
void add_hours(int nb_hours);
void add_minutes(int nb_minutes);
void add_seconds(int nb_seconds);
bool is_leapyear();
static datetime parse(string format, string value); 
//NB: At this time only the following format specifiers (yyyy, MM, dd, HH, hh, mm, ss and tt) 
//    are supported by the parse method.
static bool is_leapyear(int year);

Supported operators

Operator Quick description Example
<< Output stream operator datetime dtTest = datetime();
cout << dtTest << endl;
< Less operator datetime dtTest = datetime(2015, 02, 14, 14, 00, 00);
datetime dtTest2 = datetime(2015, 02, 14, 14, 00, 01);
assert(dtTest < dtTest2);
<= Less equal operator datetime dtTest = datetime(2015, 02, 14, 14, 00, 00);
datetime dtTest2 = datetime(2015, 02, 14, 14, 00, 01);
assert(dtTest <= dtTest2);
> Greater operator datetime dtTest = datetime(2015, 02, 14, 14, 00, 01);
datetime dtTest2 = datetime(2015, 02, 14, 14, 00, 00);
assert(dtTest > dtTest2);
>= Greater equal operator datetime dtTest = datetime(2015, 02, 14, 14, 00, 01);
datetime dtTest2 = datetime(2015, 02, 14, 14, 00, 00);
assert(dtTest >= dtTest2);
== Equality operator assert(datetime(2017, 1, 8, 15, 28, 23) == datetime(2017, 1, 8, 15, 28, 23));
!= Inequality operator assert(datetime(2017, 1, 8, 15, 28, 23) != datetime(2017, 1, 8, 15, 28, 22));
- Minus operator datetime dtTest = datetime(2015, 02, 11, 13, 00, 00);
datetime dtTest2 = datetime(2015, 02, 14, 15, 00, 00);
timespan tsTest = dtTest2 - dtTest;
assert(tsTest.get_days() == 3);
assert(tsTest.get_hours() == 2);

Custom Date and Time Format Strings

Format specifier Description
yyyy The year as a four-digit number.
yy The year as a two-digit number.
M The month, from 1 through 12.
MM The month, from 01 through 12.
d The day of the month, from 1 through 31.
dd The day of the month, from 01 through 31.
h The hour, using a 12-hour clock from 1 to 12.
hh The hour, using a 12-hour clock from 01 to 12.
H The hour, using a 24-hour clock from 0 to 23.
HH The hour, using a 24-hour clock from 00 to 23.
m The minute, from 0 through 59.
mm The minute, from 00 through 59.
s The second, from 0 through 59.
ss The second, from 00 through 59.
tt The period (AM/PM designator).

Enumerators

enum weekday { sunday, monday, tuesday, wednesday, thursday, friday, saturday };
enum period { undefined, AM, PM };  //undefined is for 24 hour format
Clone this wiki locally