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.Date

Represents a date in the Proleptic Gregorian Calendar ranging from 32,768 B.C. to 32,767 A.D. Positive years are A.D. Non-positive years are B.C.

struct Date ;

Year, month, and day are kept separately internally so that 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 per ISO 8601, it treats 1 B.C. as year 0, i.e. 1 B.C. is 0, 2 B.C. is -1, etc. Use yearBC 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.

Constructors

NameDescription
this
this

Properties

NameTypeDescription
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 Date is on.
dayOfGregorianCal[set] intThe Xth day of the Gregorian Calendar that this Date is on.
dayOfWeek[get] DayOfWeekDay of the week this Date is on.
dayOfYear[get] ushortDay of the year this Date is on.
dayOfYear[set] intDay of the year.
daysInMonth[get] ubyteThe last day in the month that this Date is in.
endOfMonth[get] DateDate for the last day in the month that this Date is in.
isAD[get] boolWhether the current year is a date in A.D.
isLeapYear[get] boolWhether this Date is in a leap year.
isoWeek[get] ubyteThe ISO 8601 week of the year that this Date is in.
julianDay[get] longThe Julian day for this Date at noon (since the Julian day changes at noon).
max[get] DateReturns the Date farthest in the future which is representable by Date.
min[get] DateReturns the Date farthest in the past which is representable by Date.
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.
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] ushortYear 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 Adds the given number of years or months to this Date, mutating it. A negative number will subtract.
diffMonths Returns the difference between the two Dates in months.
fromISOExtString Creates a Date from a string with the format YYYY-MM-DD. Whitespace is stripped from the given string.
fromISOString Creates a Date from a string with the format YYYYMMDD. Whitespace is stripped from the given string.
fromSimpleString Creates a Date from a string with the format YYYY-Mon-DD. Whitespace is stripped from the given string.
opBinary Gives the result of adding or subtracting a Duration from
opBinary Gives the difference between two Dates.
opCmp Compares this Date with the given Date.
opOpAssign Gives the result of adding or subtracting a Duration from this Date, as well as assigning the result to this Date.
roll Adds the given number of years or months to this Date, mutating it. A negative number will subtract.
roll Adds the given number of units to this Date, mutating it. A negative number will subtract.
toISOExtString Converts this Date to a string with the format YYYY-MM-DD. If writer is set, the resulting string will be written directly to it.
toISOString Converts this Date to a string with the format YYYYMMDD. If writer is set, the resulting string will be written directly to it.
toSimpleString Converts this Date to a string with the format YYYY-Mon-DD. If writer is set, the resulting string will be written directly to it.
toString Converts this Date to a string.

Example

import core.time : days;

auto d = Date(2000, 6, 1);

writeln(d.dayOfYear); // 153
writeln(d.dayOfWeek); // DayOfWeek.thu

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

Authors

Jonathan M Davis

License

Boost License 1.0.