date date and time library

date date and time library

date date and time library

Function

date() creates a local time zone Date object; supports date string parsing, chrono/strftime style formatting, Unix timestamp conversion, date operations, and reading of fields such as year, month, day, hour, minute, and second.

Syntax

Parameters

ParameterTypeRequiredDefault valueDescription
timestampInt/FloatNoNoneSecond level Unix timestamp; if no parameters are passed, the current local time is returned.
textStringNoNoneDate string or second-level Unix timestamp string.

Return Value

TypeDescription
DateReturns the local time zone Date object.

API List

APIDescription
Date.formatOutput date and time according to format string.
Date.from_stringParses a date string and creates a new Date object.
Date.from_timestampCreates a new Date object from a Unix timestamp in seconds.
Date.from_millisCreate a new Date object from a millisecond Unix timestamp.
Date.from_microsCreates a new Date object from a microsecond Unix timestamp.
Date.from_nanosCreates a new Date object from a nanosecond Unix timestamp.
Date.addReturns a new Date object plus or minus the specified time.
Date.diffReturns the difference between two dates converted in the specified unit.
Date.start_of_dayReturns a new Date object at zero o'clock on the day.
Date.timestampRead second-level Unix timestamp.
Date.timestamp_millisRead millisecond-level Unix timestamp.
Date.timestamp_microsRead microsecond-level Unix timestamp.
Date.timestamp_nanosRead a nanosecond Unix timestamp.
Date.yearRead the year.
Date.monthRead the month.
Date.dayRead the day of the month.
Date.hourRead the hour.
Date.minuteRead minutes.
Date.secondRead seconds.
Date.millisRead the millisecond part of the current second.
Date.microsRead the microsecond part of the current second.
Date.nanosRead the nanosecond part of the current second.
Date.weekdayRead the weekday number.
Date.weekRead the ISO week number.
Date.yeardayRead the date serial number in a year.
Date.quarterRead quarter.
Date.to_stringOutput the date and time in the default format.

Code Examples

Notes

  • No parameters date() returns the current local time.
- Integer and floating point arguments create a Date in seconds Unix timestamp.
  • String parameters will be parsed into Date according to common date formats, and date('%Y-%m-%d') is no longer supported to directly format the current time.
  • To format the current time, use date().format('%Y-%m-%d').
- Date uses the local time zone internally and uses the first local time result when daylight saving time ambiguities are encountered.