The Date
object to convert
new DataAPIDate(new Date('2004-09-14T12:00:00.000')) // '2004-09-14'
date(new Date('-200004-09-14')) // '200004-09-14'
Parses a DataAPIDate
from a string in the format [+-]?YYY(Y+)-MM-DD
.
See DataAPIDate for more info about the exact format.
The date to parse
new DataAPIDate('2004-09-14') // '2004-09-14'
date('-2004-09-14') // '-2004-09-14'
date('+123456-09-14') // '123456-09-14'
Internal
Should not be called by user directly.
Creates a DataAPIDate
from a year, a month, and a date.
The year may be negative. The month and day are both 1-indexed.
The date must be valid for the given month, otherwise an exception will be thrown.
The year to use
The month to use (1-indexed)
The date to use (1-indexed)
new DataAPIDate(2004, 9, 14) // '2004-09-14'
date(-200004, 9, 14) // '-200004-09-14'
Readonly
dateThe date component of this DataAPIDate
.
Must be a valid day for the given month.
Readonly
monthThe month component of this DataAPIDate
.
Must be between 1-12.
Readonly
yearThe year component of this DataAPIDate
.
May be negative.
Implementation of $SerializeForTable
for TableCodec
The other DataAPIDate
to compare to
0
if the dates are equal, -1
if this date is before the other, and 1
if this date is after the other
date('2004-09-14').compare(date(2004, 9, 14)) // 0
date('2004-09-14').compare(date(2004, 9, 15)) // -1
date('2004-09-15').compare(date(2004, 9, 14)) // 1
The other DataAPIDate
to compare to
true
if the dates are equal, and false
otherwise
date('2004-09-14').equals(date(2004, 9, 14)) // true
date('2004-09-14').equals(date(2004, 9, 15)) // false
date('2004-09-15').equals(date(2004, 9, 14)) // false
Adds a DataAPIDuration or a duration string to this DataAPIDate
.
Duration strings will be parsed into a DataAPIDuration before being added.
Durations may be negative, in which case the duration will be subtracted from this date.
The duration to add to this DataAPIDate
A new DataAPIDate
representing the result of adding the duration to this date
date('2000-01-01').plus('1y47h59m') // '2001-01-02'
date('2000-01-01').plus('365d') // '2000-12-31'
date('2000-01-01').plus('366d') // '2001-01-01'
Converts this DataAPIDate
to a Date
object in the local timezone.
If no base
date/time is provided, the time component defaults to 00:00:00
(local time).
If the base
parameter is a DataAPITime
, it is interpreted as being in the local timezone, not UTC.
See DataAPIDate.toDateUTC for a UTC-based alternative to this method.
Optional
base: Date | DataAPITimeThe base date/time to use for the time component. If omitted, defaults to 00:00:00
(local time).
The Date
object representing this DataAPIDate
in the local timezone.
// Assuming the local timezone is UTC-6 (CST) //
// Local Time: '2000-01-01T12:00:00'
// UTC Time: '2000-01-01T18:00:00Z'
date('2000-01-01').toDate(new Date('1970-01-01T12:00:00'));
// Local Time: '2000-01-01T06:00:00'
// UTC Time: '2000-01-01T12:00:00Z'
date('2000-01-01').toDate(new Date('1970-01-01T12:00:00Z'));
// Local Time: '2000-01-01T12:00:00'
// UTC Time: '2000-01-01T18:00:00Z'
date('2000-01-01').toDate(new DataAPITime('12:00:00'));
// Local Time: '2000-01-01T00:00:00'
// UTC Time: '2000-01-01T06:00:00Z'
date('2000-01-01').toDate();
DataAPIDate.toDateUTC
Converts this DataAPIDate
to a Date
object in UTC.
If no base
date/time is provided, the time component defaults to 00:00:00
(UTC).
If the base
parameter is a DataAPITime
, it is interpreted as being in the UTC timezone.
See DataAPIDate.toDate for a local-time-based alternative to this method.
Optional
base: Date | DataAPITimeThe base time to use for the time component. If omitted, defaults to 00:00:00
UTC.
The Date
object representing this DataAPIDate
in UTC.
// Assuming the local timezone is UTC-6 (CST) //
// Local Time: '2000-01-01T12:00:00'
// UTC Time: '2000-01-01T18:00:00Z'
date('2000-01-01').toDateUTC(new Date('1970-01-01T12:00:00'));
// Local Time: '2000-01-01T06:00:00'
// UTC Time: '2000-01-01T12:00:00Z'
date('2000-01-01').toDateUTC(new Date('1970-01-01T12:00:00Z'));
// Local Time: '2000-01-01T12:00:00'
// UTC Time: '2000-01-01T18:00:00Z'
date('2000-01-01').toDateUTC(new DataAPITime('12:00:00'));
// Local Time: '1999-12-31T18:00:00'
// UTC Time: '2000-01-01T00:00:00Z'
date('2000-01-01').toDateUTC();
DataAPIDate.toDate
Returns the string representation of this DataAPIDate
Note that a +
is prepended to the year if it is greater than or equal to 10000.
The string representation of this DataAPIDate
date('2004-09-14').toString() // '2004-09-14'
date(-2004, 9, 14).toString() // '-2004-09-14'
date('123456-01-01').toString() // '+123456-01-01'
Static
[$Implementation of $DeserializeForTable
for TableCodec
Static
nowThe current date in the local timezone
const now = date.now();
// or
const now = DataAPIDate.now()
Static
ofCreates a DataAPIDate
from the number of days since the epoch.
The number may be negative, but must be an integer within the range [-100_000_000, 100_000_000]
.
The number of days since the epoch (may be negative)
The date representing the given number of days since the epoch
DataAPIDate.ofEpochDay(0) // '1970-01-01'
date.ofEpochDay(12675) // '2004-09-14'
date.ofEpochDay(-1) // '1969-12-31'
Static
ofCreates a DataAPIDate
from a year and a valid day of the year.
The year may be negative.
The day-of-year must be valid for the year, otherwise an exception will be thrown.
The year to use
The day of the year to use (1-indexed)
The date representing the given year and day of the year
DataAPIDate.ofYearDay(2004, 258) // 2004-09-14
date.ofYearDay(2004, 1) // 2004-01-01
date.ofYearDay(2004, 366) // 2004-12-31 (ok b/c 2004 is a leap year)
Static
utcnowThe current date in UTC
const now = date.utcnow();
// or
const now = DataAPIDate.utcnow()
Overview
Represents a
date
column for Data API tables,Format
date
s consist simply of a year, a month, and a date.The year may be either positive or negative, and must be at least four digits long (with leading padding zeros if necessary).
The month must be between 1-12 (not zero-indexed like JS dates), and must be two digits long.
The day must be a valid day for the given month, and starts at 1. It must also be two digits long. Feb 29th is allowed on leap years
Together, the hypothetical pseudo-regex would be as such:
[+-]?YYY(Y+)-MM-DD
.Note that the
DataAPIDate
's parser is lenient on if the leading+
is included or not. For example,+2000-01-01
is accepted, even if it is not technically valid; same with10000-01-01
. A plus will be prepended in DataAPIDate.toString as necessary.Creation
There are a number of different ways to initialize a
DataAPIDate
:Example
The
date
shorthandYou may use the date shorthand function-object anywhere when creating new
DataAPIDate
s.Example
See the official DataStax documentation for more information.
See
date