| Category | Functions |
|---|---|
| Main date types | DateDateTime |
| Other date types | MonthDayOfWeekTimeOfDay |
| Date checking | validvalidTimeUnitsyearIsLeapYearisTimePointenforceValid |
| Date conversion | daysToDayOfWeekmonthsToMonth |
| Time units | cmpTimeUnitstimeStrings |
| Other | AllowDayOverflowDateTimeException |
Sourcestd/datetime/date.d
DateTimeException = core.time.TimeException;Month: ubyte;writeln(Date(2018, 10, 1).month);// Month.octwriteln(DateTime(1, 1, 1).month);// Month.jan
janfebmaraprmayjunjulaugsepoctnovdecDayOfWeek: ubyte;writeln(Date(2018, 10, 1).dayOfWeek);// DayOfWeek.monwriteln(DateTime(5, 5, 5).dayOfWeek);// DayOfWeek.thu
sunmontuewedthufrisatAllowDayOverflow = std.typecons.Flag!"allowDayOverflow".Flag;AllowDayOverflow.no, then day overflow is not allowed. Otherwise, if set toAllowDayOverflow.yes, then day overflow is allowed.timeStrings;DateTime;DateTime is intended primarily for calendar-based uses rather than precise time operations.import core.time : days, seconds;auto dt =DateTime(2000, 6, 1, 10, 30, 0);writeln(dt.date);// Date(2000, 6, 1)writeln(dt.timeOfDay);// TimeOfDay(10, 30, 0)writeln(dt.dayOfYear);// 153writeln(dt.dayOfWeek);// DayOfWeek.thudt += 10.days + 100.seconds;writeln(dt);// DateTime(2000, 6, 11, 10, 31, 40)writeln(dt.toISOExtString());// "2000-06-11T10:31:40"writeln(dt.toISOString());// "20000611T103140"writeln(dt.toSimpleString());// "2000-Jun-11 10:31:40"writeln(DateTime.fromISOExtString("2018-01-01T12:00:00"));// DateTime(2018, 1, 1, 12, 0, 0)writeln(DateTime.fromISOString("20180101T120000"));// DateTime(2018, 1, 1, 12, 0, 0)writeln(DateTime.fromSimpleString("2018-Jan-01 12:00:00"));// DateTime(2018, 1, 1, 12, 0, 0)
date, TimeOfDaytod = TimeOfDay.init);year, intmonth, intday, inthour = 0, intminute = 0, intsecond = 0);intyear | The year portion of the date. |
intmonth | The month portion of the date (January is 1). |
intday | The day portion of the date. |
inthour | The hour portion of the time; |
intminute | The minute portion of the time; |
intsecond | The second portion of the time; |
opCmp(DateTimerhs) const;| this < rhs | < 0 |
| this == rhs | 0 |
| this > rhs | > 0 |
date() const;date(Datedate);Datedate | The Date to set thisDateTime's date portion to. |
timeOfDay() const;timeOfDay(TimeOfDaytod);TimeOfDaytod | Thestd.datetime.date.TimeOfDay to set thisDateTime's time portion to. |
year() const;year(intyear);intyear | The year to set thisDateTime's year to. |
writeln(DateTime(Date(1999, 7, 6), TimeOfDay(9, 7, 5)).year);// 1999writeln(DateTime(Date(2010, 10, 4), TimeOfDay(0, 0, 30)).year);// 2010writeln(DateTime(Date(-7, 4, 5), TimeOfDay(7, 45, 2)).year);// -7
yearBC() const;writeln(DateTime(Date(0, 1, 1), TimeOfDay(12, 30, 33)).yearBC);// 1writeln(DateTime(Date(-1, 1, 1), TimeOfDay(10, 7, 2)).yearBC);// 2writeln(DateTime(Date(-100, 1, 1), TimeOfDay(4, 59, 0)).yearBC);// 101
yearBC(intyear);intyear | The year B.C. to set thisDateTime's year to. |
auto dt = DateTime(Date(2010, 1, 1), TimeOfDay(7, 30, 0));dt.yearBC = 1;writeln(dt);// DateTime(Date(0, 1, 1), TimeOfDay(7, 30, 0))dt.yearBC = 10;writeln(dt);// DateTime(Date(-9, 1, 1), TimeOfDay(7, 30, 0))
month() const;writeln(DateTime(Date(1999, 7, 6), TimeOfDay(9, 7, 5)).month);// 7writeln(DateTime(Date(2010, 10, 4), TimeOfDay(0, 0, 30)).month);// 10writeln(DateTime(Date(-7, 4, 5), TimeOfDay(7, 45, 2)).month);// 4
month(Monthmonth);Monthmonth | The month to set thisDateTime's month to. |
day() const;writeln(DateTime(Date(1999, 7, 6), TimeOfDay(9, 7, 5)).day);// 6writeln(DateTime(Date(2010, 10, 4), TimeOfDay(0, 0, 30)).day);// 4writeln(DateTime(Date(-7, 4, 5), TimeOfDay(7, 45, 2)).day);// 5
day(intday);intday | The day of the month to set thisDateTime's day to. |
hour() const;hour(inthour);inthour | The hour of the day to set thisDateTime's hour to. |
minute() const;minute(intminute);intminute | The minute to set thisDateTime's minute to. |
second() const;second(intsecond);intsecond | The second to set thisDateTime's second to. |
add(string units)(longvalue, AllowDayOverflowallowOverflow = AllowDayOverflow.yes)| units | The type of units to add ("years" or "months"). |
longvalue | The number of months or years to add to thisDateTime. |
AllowDayOverflowallowOverflow | Whether the days should be allowed to overflow, causing the month to increment. |
auto dt1 = DateTime(2010, 1, 1, 12, 30, 33);dt1.add!"months"(11);writeln(dt1);// DateTime(2010, 12, 1, 12, 30, 33)auto dt2 = DateTime(2010, 1, 1, 12, 30, 33);dt2.add!"months"(-11);writeln(dt2);// DateTime(2009, 2, 1, 12, 30, 33)auto dt3 = DateTime(2000, 2, 29, 12, 30, 33);dt3.add!"years"(1);writeln(dt3);// DateTime(2001, 3, 1, 12, 30, 33)auto dt4 = DateTime(2000, 2, 29, 12, 30, 33);dt4.add!"years"(1, AllowDayOverflow.no);writeln(dt4);// DateTime(2001, 2, 28, 12, 30, 33)
roll(string units)(longvalue, AllowDayOverflowallowOverflow = AllowDayOverflow.yes)| units | The type of units to add ("years" or "months"). |
longvalue | The number of months or years to add to thisDateTime. |
AllowDayOverflowallowOverflow | Whether the days should be allowed to overflow, causing the month to increment. |
auto dt1 = DateTime(2010, 1, 1, 12, 33, 33);dt1.roll!"months"(1);writeln(dt1);// DateTime(2010, 2, 1, 12, 33, 33)auto dt2 = DateTime(2010, 1, 1, 12, 33, 33);dt2.roll!"months"(-1);writeln(dt2);// DateTime(2010, 12, 1, 12, 33, 33)auto dt3 = DateTime(1999, 1, 29, 12, 33, 33);dt3.roll!"months"(1);writeln(dt3);// DateTime(1999, 3, 1, 12, 33, 33)auto dt4 = DateTime(1999, 1, 29, 12, 33, 33);dt4.roll!"months"(1, AllowDayOverflow.no);writeln(dt4);// DateTime(1999, 2, 28, 12, 33, 33)auto dt5 = DateTime(2000, 2, 29, 12, 30, 33);dt5.roll!"years"(1);writeln(dt5);// DateTime(2001, 3, 1, 12, 30, 33)auto dt6 = DateTime(2000, 2, 29, 12, 30, 33);dt6.roll!"years"(1, AllowDayOverflow.no);writeln(dt6);// DateTime(2001, 2, 28, 12, 30, 33)
roll(string units)(longvalue)roll(string units)(longvalue)| units | The units to add. |
longvalue | The number ofunits to add to thisDateTime. |
auto dt1 = DateTime(2010, 1, 1, 11, 23, 12);dt1.roll!"days"(1);writeln(dt1);// DateTime(2010, 1, 2, 11, 23, 12)dt1.roll!"days"(365);writeln(dt1);// DateTime(2010, 1, 26, 11, 23, 12)dt1.roll!"days"(-32);writeln(dt1);// DateTime(2010, 1, 25, 11, 23, 12)auto dt2 = DateTime(2010, 7, 4, 12, 0, 0);dt2.roll!"hours"(1);writeln(dt2);// DateTime(2010, 7, 4, 13, 0, 0)auto dt3 = DateTime(2010, 1, 1, 0, 0, 0);dt3.roll!"seconds"(-1);writeln(dt3);// DateTime(2010, 1, 1, 0, 0, 59)
opBinary(string op)(Durationduration) constopBinaryRight(string op)(Durationduration) const| DateTime | + | Duration | --> | DateTime |
| DateTime | - | Duration | --> | DateTime |
| Duration | + | DateTime | --> | DateTime |
Durationduration | Thecore.time.Duration to add to or subtract from thisDateTime. |
import core.time : hours, seconds;assert(DateTime(2015, 12, 31, 23, 59, 59) + seconds(1) == DateTime(2016, 1, 1, 0, 0, 0));assert(DateTime(2015, 12, 31, 23, 59, 59) + hours(1) == DateTime(2016, 1, 1, 0, 59, 59));assert(DateTime(2016, 1, 1, 0, 0, 0) - seconds(1) == DateTime(2015, 12, 31, 23, 59, 59));assert(DateTime(2016, 1, 1, 0, 59, 59) - hours(1) == DateTime(2015, 12, 31, 23, 59, 59));assert(DateTime(2015, 12, 31, 23, 59, 59) + hours(1) == hours(1) + DateTime(2015, 12, 31, 23, 59, 59));
opOpAssign(string op)(Durationduration)| DateTime | + | duration | --> | DateTime |
| DateTime | - | duration | --> | DateTime |
Durationduration | The duration to add to or subtract from thisDateTime. |
opBinary(string op)(DateTimerhs) const| DateTime | - | DateTime | --> | duration |
diffMonths(DateTimerhs) const;DateTimerhs | TheDateTime to subtract from this one. |
assert(DateTime(1999, 2, 1, 12, 2, 3).diffMonths( DateTime(1999, 1, 31, 23, 59, 59)) == 1);assert(DateTime(1999, 1, 31, 0, 0, 0).diffMonths( DateTime(1999, 2, 1, 12, 3, 42)) == -1);assert(DateTime(1999, 3, 1, 5, 30, 0).diffMonths( DateTime(1999, 1, 1, 2, 4, 7)) == 2);assert(DateTime(1999, 1, 1, 7, 2, 4).diffMonths( DateTime(1999, 3, 31, 0, 30, 58)) == -2);
isLeapYear() const;dayOfWeek() const;dayOfYear() const;writeln(DateTime(Date(1999, 1, 1), TimeOfDay(12, 22, 7)).dayOfYear);// 1writeln(DateTime(Date(1999, 12, 31), TimeOfDay(7, 2, 59)).dayOfYear);// 365writeln(DateTime(Date(2000, 12, 31), TimeOfDay(21, 20, 0)).dayOfYear);// 366
dayOfYear(intday);intday | The day of the year to set which day of the year thisDateTime is on. |
dayOfGregorianCal() const;writeln(DateTime(Date(1, 1, 1), TimeOfDay(0, 0, 0)).dayOfGregorianCal);// 1writeln(DateTime(Date(1, 12, 31), TimeOfDay(23, 59, 59)).dayOfGregorianCal);// 365writeln(DateTime(Date(2, 1, 1), TimeOfDay(2, 2, 2)).dayOfGregorianCal);// 366writeln(DateTime(Date(0, 12, 31), TimeOfDay(7, 7, 7)).dayOfGregorianCal);// 0writeln(DateTime(Date(0, 1, 1), TimeOfDay(19, 30, 0)).dayOfGregorianCal);// -365writeln(DateTime(Date(-1, 12, 31), TimeOfDay(4, 7, 0)).dayOfGregorianCal);// -366writeln(DateTime(Date(2000, 1, 1), TimeOfDay(9, 30, 20)).dayOfGregorianCal);// 730_120writeln(DateTime(Date(2010, 12, 31), TimeOfDay(15, 45, 50)).dayOfGregorianCal);// 734_137
dayOfGregorianCal(intdays);intdays | The day of the Gregorian Calendar to set thisDateTime to. |
auto dt = DateTime(Date.init, TimeOfDay(12, 0, 0));dt.dayOfGregorianCal = 1;writeln(dt);// DateTime(Date(1, 1, 1), TimeOfDay(12, 0, 0))dt.dayOfGregorianCal = 365;writeln(dt);// DateTime(Date(1, 12, 31), TimeOfDay(12, 0, 0))dt.dayOfGregorianCal = 366;writeln(dt);// DateTime(Date(2, 1, 1), TimeOfDay(12, 0, 0))dt.dayOfGregorianCal = 0;writeln(dt);// DateTime(Date(0, 12, 31), TimeOfDay(12, 0, 0))dt.dayOfGregorianCal = -365;writeln(dt);// DateTime(Date(-0, 1, 1), TimeOfDay(12, 0, 0))dt.dayOfGregorianCal = -366;writeln(dt);// DateTime(Date(-1, 12, 31), TimeOfDay(12, 0, 0))dt.dayOfGregorianCal = 730_120;writeln(dt);// DateTime(Date(2000, 1, 1), TimeOfDay(12, 0, 0))dt.dayOfGregorianCal = 734_137;writeln(dt);// DateTime(Date(2010, 12, 31), TimeOfDay(12, 0, 0))
isoWeek() const;isoWeekYear() const;endOfMonth() const;assert(DateTime(Date(1999, 1, 6), TimeOfDay(0, 0, 0)).endOfMonth == DateTime(Date(1999, 1, 31), TimeOfDay(23, 59, 59)));assert(DateTime(Date(1999, 2, 7), TimeOfDay(19, 30, 0)).endOfMonth == DateTime(Date(1999, 2, 28), TimeOfDay(23, 59, 59)));assert(DateTime(Date(2000, 2, 7), TimeOfDay(5, 12, 27)).endOfMonth == DateTime(Date(2000, 2, 29), TimeOfDay(23, 59, 59)));assert(DateTime(Date(2000, 6, 4), TimeOfDay(12, 22, 9)).endOfMonth == DateTime(Date(2000, 6, 30), TimeOfDay(23, 59, 59)));
daysInMonth() const;writeln(DateTime(Date(1999, 1, 6), TimeOfDay(0, 0, 0)).daysInMonth);// 31writeln(DateTime(Date(1999, 2, 7), TimeOfDay(19, 30, 0)).daysInMonth);// 28writeln(DateTime(Date(2000, 2, 7), TimeOfDay(5, 12, 27)).daysInMonth);// 29writeln(DateTime(Date(2000, 6, 4), TimeOfDay(12, 22, 9)).daysInMonth);// 30
isAD() const;assert(DateTime(Date(1, 1, 1), TimeOfDay(12, 7, 0)).isAD);assert(DateTime(Date(2010, 12, 31), TimeOfDay(0, 0, 0)).isAD);assert(!DateTime(Date(0, 12, 31), TimeOfDay(23, 59, 59)).isAD);assert(!DateTime(Date(-2010, 1, 1), TimeOfDay(2, 2, 2)).isAD);
julianDay() const;modJulianDay() const;toISOString() const;toISOString(W)(ref Wwriter) constwriter is set, the resulting string will be written directly to it.Wwriter | Achar acceptingoutput range |
assert(DateTime(Date(2010, 7, 4), TimeOfDay(7, 6, 12)).toISOString() =="20100704T070612");assert(DateTime(Date(1998, 12, 25), TimeOfDay(2, 15, 0)).toISOString() =="19981225T021500");assert(DateTime(Date(0, 1, 5), TimeOfDay(23, 9, 59)).toISOString() =="00000105T230959");assert(DateTime(Date(-4, 1, 5), TimeOfDay(0, 0, 2)).toISOString() =="-00040105T000002");
toISOExtString() const;toISOExtString(W)(ref Wwriter) constwriter is set, the resulting string will be written directly to it.Wwriter | Achar acceptingoutput range |
assert(DateTime(Date(2010, 7, 4), TimeOfDay(7, 6, 12)).toISOExtString() =="2010-07-04T07:06:12");assert(DateTime(Date(1998, 12, 25), TimeOfDay(2, 15, 0)).toISOExtString() =="1998-12-25T02:15:00");assert(DateTime(Date(0, 1, 5), TimeOfDay(23, 9, 59)).toISOExtString() =="0000-01-05T23:09:59");assert(DateTime(Date(-4, 1, 5), TimeOfDay(0, 0, 2)).toISOExtString() =="-0004-01-05T00:00:02");
toSimpleString() const;toSimpleString(W)(ref Wwriter) constwriter is set, the resulting string will be written directly to it.Wwriter | Achar acceptingoutput range |
assert(DateTime(Date(2010, 7, 4), TimeOfDay(7, 6, 12)).toSimpleString() =="2010-Jul-04 07:06:12");assert(DateTime(Date(1998, 12, 25), TimeOfDay(2, 15, 0)).toSimpleString() =="1998-Dec-25 02:15:00");assert(DateTime(Date(0, 1, 5), TimeOfDay(23, 9, 59)).toSimpleString() =="0000-Jan-05 23:09:59");assert(DateTime(Date(-4, 1, 5), TimeOfDay(0, 0, 2)).toSimpleString() =="-0004-Jan-05 00:00:02");
toString() const;toString(W)(ref Wwriter) constfromISOString(S)(scope const SisoString)SisoString | A string formatted in the ISO format for dates and times. |
fromISOExtString(S)(scope const SisoExtString)SisoExtString | A string formatted in the ISO Extended format for dates and times. |
fromSimpleString(S)(scope const SsimpleString)SsimpleString | A string formatted in the way that toSimpleString formats dates and times. |
min();max();Date;Date is optimized for calendar-based operations.Date uses the Proleptic Gregorian Calendar, so it assumes the Gregorian leap year calculations for its entire length. As perISO 8601, it treats 1 B.C. as year 0, i.e. 1 B.C. is 0, 2 B.C. is -1, etc. UseyearBC to use B.C. as a positive integer with 1 B.C. being the year prior to 1 A.D. Year 0 is a leap year.import core.time : days;auto d =Date(2000, 6, 1);writeln(d.dayOfYear);// 153writeln(d.dayOfWeek);// DayOfWeek.thud += 10.days;writeln(d);// Date(2000, 6, 11)writeln(d.toISOExtString());// "2000-06-11"writeln(d.toISOString());// "20000611"writeln(d.toSimpleString());// "2000-Jun-11"writeln(Date.fromISOExtString("2018-01-01"));// Date(2018, 1, 1)writeln(Date.fromISOString("20180101"));// Date(2018, 1, 1)writeln(Date.fromSimpleString("2018-Jan-01"));// Date(2018, 1, 1)
year, intmonth, intday);intyear | Year of the Gregorian Calendar. Positive values are A.D. Non-positive values are B.C. with year 0 being the year prior to 1 A.D. |
intmonth | Month of the year (January is 1). |
intday | Day of the month. |
day);intday | The Xth day of the Gregorian Calendar that the constructedDate will be for. |
opCmp(Daterhs) const;| this < rhs | < 0 |
| this == rhs | 0 |
| this > rhs | > 0 |
year() const;writeln(Date(1999, 7, 6).year);// 1999writeln(Date(2010, 10, 4).year);// 2010writeln(Date(-7, 4, 5).year);// -7
year(intyear);intyear | The year to set this Date's year to. |
writeln(Date(1999, 7, 6).year);// 1999writeln(Date(2010, 10, 4).year);// 2010writeln(Date(-7, 4, 5).year);// -7
yearBC() const;writeln(Date(0, 1, 1).yearBC);// 1writeln(Date(-1, 1, 1).yearBC);// 2writeln(Date(-100, 1, 1).yearBC);// 101
yearBC(intyear);intyear | The year B.C. to set thisDate's year to. |
auto date = Date(2010, 1, 1);date.yearBC = 1;writeln(date);// Date(0, 1, 1)date.yearBC = 10;writeln(date);// Date(-9, 1, 1)
month() const;writeln(Date(1999, 7, 6).month);// 7writeln(Date(2010, 10, 4).month);// 10writeln(Date(-7, 4, 5).month);// 4
month(Monthmonth);Monthmonth | The month to set thisDate's month to. |
day() const;writeln(Date(1999, 7, 6).day);// 6writeln(Date(2010, 10, 4).day);// 4writeln(Date(-7, 4, 5).day);// 5
day(intday);intday | The day of the month to set thisDate's day to. |
add(string units)(longvalue, AllowDayOverflowallowOverflow = AllowDayOverflow.yes)| units | The type of units to add ("years" or "months"). |
longvalue | The number of months or years to add to thisDate. |
AllowDayOverflowallowOverflow | Whether the day should be allowed to overflow, causing the month to increment. |
auto d1 = Date(2010, 1, 1);d1.add!"months"(11);writeln(d1);// Date(2010, 12, 1)auto d2 = Date(2010, 1, 1);d2.add!"months"(-11);writeln(d2);// Date(2009, 2, 1)auto d3 = Date(2000, 2, 29);d3.add!"years"(1);writeln(d3);// Date(2001, 3, 1)auto d4 = Date(2000, 2, 29);d4.add!"years"(1, AllowDayOverflow.no);writeln(d4);// Date(2001, 2, 28)
roll(string units)(longvalue, AllowDayOverflowallowOverflow = AllowDayOverflow.yes)| units | The type of units to add ("years" or "months"). |
longvalue | The number of months or years to add to thisDate. |
AllowDayOverflowallowOverflow | Whether the day should be allowed to overflow, causing the month to increment. |
auto d1 = Date(2010, 1, 1);d1.roll!"months"(1);writeln(d1);// Date(2010, 2, 1)auto d2 = Date(2010, 1, 1);d2.roll!"months"(-1);writeln(d2);// Date(2010, 12, 1)auto d3 = Date(1999, 1, 29);d3.roll!"months"(1);writeln(d3);// Date(1999, 3, 1)auto d4 = Date(1999, 1, 29);d4.roll!"months"(1, AllowDayOverflow.no);writeln(d4);// Date(1999, 2, 28)auto d5 = Date(2000, 2, 29);d5.roll!"years"(1);writeln(d5);// Date(2001, 3, 1)auto d6 = Date(2000, 2, 29);d6.roll!"years"(1, AllowDayOverflow.no);writeln(d6);// Date(2001, 2, 28)
roll(string units)(longdays)days");days".| units | The units to add. Must be"days". |
longdays | The number of days to add to thisDate. |
auto d = Date(2010, 1, 1);d.roll!"days"(1);writeln(d);// Date(2010, 1, 2)d.roll!"days"(365);writeln(d);// Date(2010, 1, 26)d.roll!"days"(-32);writeln(d);// Date(2010, 1, 25)
opBinary(string op)(Durationduration) constopBinaryRight(string op)(Durationduration) const| Date | + | Duration | --> | Date |
| Date | - | Duration | --> | Date |
| Duration | + | Date | --> | Date |
Durationduration | Thecore.time.Duration to add to or subtract from thisDate. |
import core.time : days;writeln(Date(2015, 12, 31) + days(1));// Date(2016, 1, 1)writeln(Date(2004, 2, 26) + days(4));// Date(2004, 3, 1)writeln(Date(2016, 1, 1) - days(1));// Date(2015, 12, 31)writeln(Date(2004, 3, 1) - days(4));// Date(2004, 2, 26)writeln(Date(2004, 2, 26) + days(4));// days(4) + Date(2004, 2, 26)
opOpAssign(string op)(Durationduration)| Date | + | Duration | --> | Date |
| Date | - | Duration | --> | Date |
Durationduration | Thecore.time.Duration to add to or subtract from thisDate. |
opBinary(string op)(Daterhs) const| Date | - | Date | --> | duration |
diffMonths(Daterhs) const;Daterhs | TheDate to subtract from this one. |
writeln(Date(1999, 2, 1).diffMonths(Date(1999, 1, 31)));// 1writeln(Date(1999, 1, 31).diffMonths(Date(1999, 2, 1)));// -1writeln(Date(1999, 3, 1).diffMonths(Date(1999, 1, 1)));// 2writeln(Date(1999, 1, 1).diffMonths(Date(1999, 3, 31)));// -2
isLeapYear() const;dayOfWeek() const;dayOfYear() const;writeln(Date(1999, 1, 1).dayOfYear);// 1writeln(Date(1999, 12, 31).dayOfYear);// 365writeln(Date(2000, 12, 31).dayOfYear);// 366
dayOfYear(intday);intday | The day of the year to set which day of the year thisDate is on. |
dayOfGregorianCal() const;writeln(Date(1, 1, 1).dayOfGregorianCal);// 1writeln(Date(1, 12, 31).dayOfGregorianCal);// 365writeln(Date(2, 1, 1).dayOfGregorianCal);// 366writeln(Date(0, 12, 31).dayOfGregorianCal);// 0writeln(Date(0, 1, 1).dayOfGregorianCal);// -365writeln(Date(-1, 12, 31).dayOfGregorianCal);// -366writeln(Date(2000, 1, 1).dayOfGregorianCal);// 730_120writeln(Date(2010, 12, 31).dayOfGregorianCal);// 734_137
dayOfGregorianCal(intday);intday | The day of the Gregorian Calendar to set thisDate to. |
auto date = Date.init;date.dayOfGregorianCal = 1;writeln(date);// Date(1, 1, 1)date.dayOfGregorianCal = 365;writeln(date);// Date(1, 12, 31)date.dayOfGregorianCal = 366;writeln(date);// Date(2, 1, 1)date.dayOfGregorianCal = 0;writeln(date);// Date(0, 12, 31)date.dayOfGregorianCal = -365;writeln(date);// Date(-0, 1, 1)date.dayOfGregorianCal = -366;writeln(date);// Date(-1, 12, 31)date.dayOfGregorianCal = 730_120;writeln(date);// Date(2000, 1, 1)date.dayOfGregorianCal = 734_137;writeln(date);// Date(2010, 12, 31)
isoWeekAndYear() const;isoWeek() const;isoWeekYear() const;endOfMonth() const;writeln(Date(1999, 1, 6).endOfMonth);// Date(1999, 1, 31)writeln(Date(1999, 2, 7).endOfMonth);// Date(1999, 2, 28)writeln(Date(2000, 2, 7).endOfMonth);// Date(2000, 2, 29)writeln(Date(2000, 6, 4).endOfMonth);// Date(2000, 6, 30)
daysInMonth() const;writeln(Date(1999, 1, 6).daysInMonth);// 31writeln(Date(1999, 2, 7).daysInMonth);// 28writeln(Date(2000, 2, 7).daysInMonth);// 29writeln(Date(2000, 6, 4).daysInMonth);// 30
isAD() const;assert(Date(1, 1, 1).isAD);assert(Date(2010, 12, 31).isAD);assert(!Date(0, 12, 31).isAD);assert(!Date(-2010, 1, 1).isAD);
julianDay() const;modJulianDay() const;toISOString() const;toISOString(W)(ref Wwriter) constwriter is set, the resulting string will be written directly to it.Wwriter | Achar acceptingoutput range |
writeln(Date(2010, 7, 4).toISOString());// "20100704"writeln(Date(1998, 12, 25).toISOString());// "19981225"writeln(Date(0, 1, 5).toISOString());// "00000105"writeln(Date(-4, 1, 5).toISOString());// "-00040105"
toISOExtString() const;toISOExtString(W)(ref Wwriter) constwriter is set, the resulting string will be written directly to it.Wwriter | Achar acceptingoutput range |
writeln(Date(2010, 7, 4).toISOExtString());// "2010-07-04"writeln(Date(1998, 12, 25).toISOExtString());// "1998-12-25"writeln(Date(0, 1, 5).toISOExtString());// "0000-01-05"writeln(Date(-4, 1, 5).toISOExtString());// "-0004-01-05"
toSimpleString() const;toSimpleString(W)(ref Wwriter) constwriter is set, the resulting string will be written directly to it.Wwriter | Achar acceptingoutput range |
writeln(Date(2010, 7, 4).toSimpleString());// "2010-Jul-04"writeln(Date(1998, 12, 25).toSimpleString());// "1998-Dec-25"writeln(Date(0, 1, 5).toSimpleString());// "0000-Jan-05"writeln(Date(-4, 1, 5).toSimpleString());// "-0004-Jan-05"
toString() const;toString(W)(ref Wwriter) constfromISOString(S)(scope const SisoString)SisoString | A string formatted in the ISO format for dates. |
fromISOExtString(S)(scope const SisoExtString)SisoExtString | A string formatted in the ISO Extended format for dates. |
fromSimpleString(S)(scope const SsimpleString)SsimpleString | A string formatted in the way that toSimpleString formats dates. |
min();max();TimeOfDay;import core.time : minutes, seconds;auto t =TimeOfDay(12, 30, 0);t += 10.minutes + 100.seconds;writeln(t);// TimeOfDay(12, 41, 40)writeln(t.toISOExtString());// "12:41:40"writeln(t.toISOString());// "124140"writeln(TimeOfDay.fromISOExtString("15:00:00"));// TimeOfDay(15, 0, 0)writeln(TimeOfDay.fromISOString("015000"));// TimeOfDay(1, 50, 0)
hour, intminute, intsecond = 0);inthour | Hour of the day [0 - 24). |
intminute | Minute of the hour [0 - 60). |
intsecond | Second of the minute [0 - 60). |
opCmp(TimeOfDayrhs) const;| this < rhs | < 0 |
| this == rhs | 0 |
| this > rhs | > 0 |
hour() const;hour(inthour);inthour | The hour of the day to set thisTimeOfDay's hour to. |
minute() const;minute(intminute);intminute | The minute to set thisTimeOfDay's minute to. |
second() const;second(intsecond);intsecond | The second to set thisTimeOfDay's second to. |
roll(string units)(longvalue)roll(string units)(longvalue)| units | The units to add. |
longvalue | The number ofunits to add to thisTimeOfDay. |
auto tod1 = TimeOfDay(7, 12, 0);tod1.roll!"hours"(1);writeln(tod1);// TimeOfDay(8, 12, 0)auto tod2 = TimeOfDay(7, 12, 0);tod2.roll!"hours"(-1);writeln(tod2);// TimeOfDay(6, 12, 0)auto tod3 = TimeOfDay(23, 59, 0);tod3.roll!"minutes"(1);writeln(tod3);// TimeOfDay(23, 0, 0)auto tod4 = TimeOfDay(0, 0, 0);tod4.roll!"minutes"(-1);writeln(tod4);// TimeOfDay(0, 59, 0)auto tod5 = TimeOfDay(23, 59, 59);tod5.roll!"seconds"(1);writeln(tod5);// TimeOfDay(23, 59, 0)auto tod6 = TimeOfDay(0, 0, 0);tod6.roll!"seconds"(-1);writeln(tod6);// TimeOfDay(0, 0, 59)
opBinary(string op)(Durationduration) constopBinaryRight(string op)(Durationduration) const| TimeOfDay | + | Duration | --> | TimeOfDay |
| TimeOfDay | - | Duration | --> | TimeOfDay |
| Duration | + | TimeOfDay | --> | TimeOfDay |
Durationduration | Thecore.time.Duration to add to or subtract from thisTimeOfDay. |
import core.time : hours, minutes, seconds;writeln(TimeOfDay(12, 12, 12) + seconds(1));// TimeOfDay(12, 12, 13)writeln(TimeOfDay(12, 12, 12) + minutes(1));// TimeOfDay(12, 13, 12)writeln(TimeOfDay(12, 12, 12) + hours(1));// TimeOfDay(13, 12, 12)writeln(TimeOfDay(23, 59, 59) + seconds(1));// TimeOfDay(0, 0, 0)writeln(TimeOfDay(12, 12, 12) - seconds(1));// TimeOfDay(12, 12, 11)writeln(TimeOfDay(12, 12, 12) - minutes(1));// TimeOfDay(12, 11, 12)writeln(TimeOfDay(12, 12, 12) - hours(1));// TimeOfDay(11, 12, 12)writeln(TimeOfDay(0, 0, 0) - seconds(1));// TimeOfDay(23, 59, 59)writeln(TimeOfDay(12, 12, 12) + seconds(1));// seconds(1) + TimeOfDay(12, 12, 12)
opOpAssign(string op)(Durationduration)| TimeOfDay | + | Duration | --> | TimeOfDay |
| TimeOfDay | - | Duration | --> | TimeOfDay |
Durationduration | Thecore.time.Duration to add to or subtract from thisTimeOfDay. |
opBinary(string op)(TimeOfDayrhs) const| TimeOfDay | - | TimeOfDay | --> | duration |
TimeOfDayrhs | TheTimeOfDay to subtract from this one. |
toISOString() const;toISOString(W)(ref Wwriter) constwriter is set, the resulting string will be written directly to it.Wwriter | Achar acceptingoutput range |
writeln(TimeOfDay(0, 0, 0).toISOString());// "000000"writeln(TimeOfDay(12, 30, 33).toISOString());// "123033"
toISOExtString() const;toISOExtString(W)(ref Wwriter) constwriter is set, the resulting string will be written directly to it.Wwriter | Achar acceptingoutput range |
writeln(TimeOfDay(0, 0, 0).toISOExtString());// "00:00:00"writeln(TimeOfDay(12, 30, 33).toISOExtString());// "12:30:33"
toString() const;toString(W)(ref Wwriter) constWwriter | Achar acceptingoutput range |
fromISOString(S)(scope const SisoString)SisoString | A string formatted in the ISO format for times. |
fromISOExtString(S)(scope const SisoExtString)SisoExtString | A string formatted in the ISO Extended format for times. |
min();max();valid(string units)(intvalue)| units | The units of time to validate. |
intvalue | The number to validate. |
assert(valid!"hours"(12));assert(!valid!"hours"(32));assert(valid!"months"(12));assert(!valid!"months"(13));
valid(string units)(intyear, intmonth, intday)| units | The units of time to validate. |
intyear | The year of the day to validate. |
intmonth | The month of the day to validate (January is 1). |
intday | The day to validate. |
assert(valid!"days"(2016, 2, 29));assert(!valid!"days"(2016, 2, 30));assert(valid!"days"(2017, 2, 20));assert(!valid!"days"(2017, 2, 29));
enforceValid(string units)(intvalue, stringfile = __FILE__, size_tline = __LINE__)| units | The units of time to validate. |
intvalue | The number to validate. |
stringfile | The file that theDateTimeException will list if thrown. |
size_tline | The line number that theDateTimeException will list if thrown. |
value) is false.import std.exception : assertThrown, assertNotThrown;assertNotThrown(enforceValid!"months"(10));assertNotThrown(enforceValid!"seconds"(40));assertThrown!DateTimeException(enforceValid!"months"(0));assertThrown!DateTimeException(enforceValid!"hours"(24));assertThrown!DateTimeException(enforceValid!"minutes"(60));assertThrown!DateTimeException(enforceValid!"seconds"(60));
enforceValid(string units)(intyear, Monthmonth, intday, stringfile = __FILE__, size_tline = __LINE__)| units | The units of time to validate. |
intyear | The year of the day to validate. |
Monthmonth | The month of the day to validate. |
intday | The day to validate. |
stringfile | The file that theDateTimeException will list if thrown. |
size_tline | The line number that theDateTimeException will list if thrown. |
import std.exception : assertThrown, assertNotThrown;assertNotThrown(enforceValid!"days"(2000, Month.jan, 1));// leap yearassertNotThrown(enforceValid!"days"(2000, Month.feb, 29));assertThrown!DateTimeException(enforceValid!"days"(2001, Month.feb, 29));assertThrown!DateTimeException(enforceValid!"days"(2000, Month.jan, 32));assertThrown!DateTimeException(enforceValid!"days"(2000, Month.apr, 31));
daysToDayOfWeek(DayOfWeekcurrDoW, DayOfWeekdow);DayOfWeekcurrDoW | The current day of the week. |
DayOfWeekdow | The day of the week to get the number of days to. |
writeln(daysToDayOfWeek(DayOfWeek.mon, DayOfWeek.mon));// 0writeln(daysToDayOfWeek(DayOfWeek.mon, DayOfWeek.sun));// 6writeln(daysToDayOfWeek(DayOfWeek.mon, DayOfWeek.wed));// 2
monthsToMonth(intcurrMonth, intmonth);intcurrMonth | The current month of the year. |
intmonth | The month of the year to get the number of months to. |
writeln(monthsToMonth(Month.jan, Month.jan));// 0writeln(monthsToMonth(Month.jan, Month.dec));// 11writeln(monthsToMonth(Month.jul, Month.oct));// 3
yearIsLeapYear(intyear);intyear | The year to to be tested. |
foreach (year; [1, 2, 100, 2001, 2002, 2003, 2005, 2006, 2007, 2009, 2010]){assert(!yearIsLeapYear(year));assert(!yearIsLeapYear(-year));}foreach (year; [0, 4, 8, 400, 800, 1600, 1996, 2000, 2004, 2008, 2012]){assert(yearIsLeapYear(year));assert(yearIsLeapYear(-year));}
isTimePoint(T);import core.time : Duration;import std.datetime.interval : Interval;import std.datetime.systime : SysTime;staticassert(isTimePoint!Date);staticassert(isTimePoint!DateTime);staticassert(isTimePoint!SysTime);staticassert(isTimePoint!TimeOfDay);staticassert(!isTimePoint!int);staticassert(!isTimePoint!Duration);staticassert(!isTimePoint!(Interval!SysTime));
validTimeUnits(string[]units...);assert(validTimeUnits("msecs","seconds","minutes"));assert(validTimeUnits("days","weeks","months"));assert(!validTimeUnits("ms","seconds","minutes"));
cmpTimeUnits(stringlhs, stringrhs);| this < rhs | < 0 |
| this == rhs | 0 |
| this > rhs | > 0 |
import std.exception : assertThrown;writeln(cmpTimeUnits("hours","hours"));// 0assert(cmpTimeUnits("hours","weeks") < 0);assert(cmpTimeUnits("months","seconds") > 0);assertThrown!DateTimeException(cmpTimeUnits("month","second"));
CmpTimeUnits(string lhs, string rhs) if (validTimeUnits(lhs, rhs))| this < rhs | < 0 |
| this == rhs | 0 |
| this > rhs | > 0 |
staticassert(CmpTimeUnits!("years","weeks") > 0);staticassert(CmpTimeUnits!("days","days") == 0);staticassert(CmpTimeUnits!("seconds","hours") < 0);