View source code
Display the source code in std/datetime/date.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Struct std.datetime.date.DateTime

Combines the Date and TimeOfDay structs to give an object which holds both the date and the time. It is optimized for calendar-based operations and has no concept of time zone. For an object which is optimized for time operations based on the system time, use SysTime. SysTime has a concept of time zone and has much higher precision (hnsecs). DateTime is intended primarily for calendar-based uses rather than precise time operations.

struct DateTime ;

Constructors

NameDescription
this (date, tod)
this (year, month, day, hour, minute, second)

Properties

NameTypeDescription
date[get] DateThe date portion of DateTime.
date[set] DateThe date portion of DateTime.
day[get] ubyteDay of a Gregorian Month.
day[set] intDay of a Gregorian Month.
dayOfGregorianCal[get] intThe Xth day of the Gregorian Calendar that this DateTime is on.
dayOfGregorianCal[set] intThe Xth day of the Gregorian Calendar that this DateTime is on. Setting this property does not affect the time portion of DateTime.
dayOfWeek[get] DayOfWeekDay of the week this DateTime is on.
dayOfYear[get] ushortDay of the year this DateTime is on.
dayOfYear[set] intDay of the year.
daysInMonth[get] ubyteThe last day in the month that this DateTime is in.
endOfMonth[get] DateTimeDateTime for the last day in the month that this DateTime is in. The time portion of endOfMonth is always 23:59:59.
hour[get] ubyteHours past midnight.
hour[set] intHours past midnight.
isAD[get] boolWhether the current year is a date in A.D.
isLeapYear[get] boolWhether this DateTime is in a leap year.
isoWeek[get] ubyteThe ISO 8601 week of the year that this DateTime is in.
isoWeekYear[get] shortThe year of the ISO 8601 week calendar that this DateTime is in.
julianDay[get] longThe Julian day for this DateTime at the given time. For example, prior to noon, 1996-03-31 would be the Julian day number 2_450_173, so this function returns 2_450_173, while from noon onward, the julian day number would be 2_450_174, so this function returns 2_450_174.
max[get] DateTimeReturns the DateTime farthest in the future which is representable by DateTime.
min[get] DateTimeReturns the DateTime farthest in the past which is representable by DateTime.
minute[get] ubyteMinutes past the hour.
minute[set] intMinutes past the hour.
modJulianDay[get] longThe modified Julian day for any time on this date (since, the modified Julian day changes at midnight).
month[get] MonthMonth of a Gregorian Year.
month[set] MonthMonth of a Gregorian Year.
second[get] ubyteSeconds past the minute.
second[set] intSeconds past the minute.
timeOfDay[get] TimeOfDayThe time portion of DateTime.
timeOfDay[set] TimeOfDayThe time portion of DateTime.
year[get] shortYear of the Gregorian Calendar. Positive numbers are A.D. Non-positive are B.C.
year[set] intYear of the Gregorian Calendar. Positive numbers are A.D. Non-positive are B.C.
yearBC[get] shortYear B.C. of the Gregorian Calendar counting year 0 as 1 B.C.
yearBC[set] intYear B.C. of the Gregorian Calendar counting year 0 as 1 B.C.

Methods

NameDescription
add (value, allowOverflow) Adds the given number of years or months to this DateTime, mutating it. A negative number will subtract.
diffMonths (rhs) Returns the difference between the two DateTimes in months.
fromISOExtString (isoExtString) Creates a DateTime from a string with the format YYYY-MM-DDTHH:MM:SS. Whitespace is stripped from the given string.
fromISOString (isoString) Creates a DateTime from a string with the format YYYYMMDDTHHMMSS. Whitespace is stripped from the given string.
fromSimpleString (simpleString) Creates a DateTime from a string with the format YYYY-Mon-DD HH:MM:SS. Whitespace is stripped from the given string.
opBinary (duration) Gives the result of adding or subtracting a Duration from this DateTime.
opBinary (rhs) Gives the difference between two DateTimes.
opCmp (rhs) Compares this DateTime with the given DateTime..
opOpAssign (duration) Gives the result of adding or subtracting a duration from this DateTime, as well as assigning the result to this DateTime.
roll (value, allowOverflow) Adds the given number of years or months to this DateTime, mutating it. A negative number will subtract.
roll (value) Adds the given number of units to this DateTime, mutating it. A negative number will subtract.
toISOExtString () Converts this DateTime to a string with the format YYYY-MM-DDTHH:MM:SS. If writer is set, the resulting string will be written directly to it.
toISOString () Converts this DateTime to a string with the format YYYYMMDDTHHMMSS. If writer is set, the resulting string will be written directly to it.
toSimpleString () Converts this DateTime to a string with the format YYYY-Mon-DD HH:MM:SS. If writer is set, the resulting string will be written directly to it.
toString () Converts this DateTime to a string.

Example

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); // 153
writeln(dt.dayOfWeek); // DayOfWeek.thu

dt += 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)

Authors

Jonathan M Davis

License

Boost License 1.0.