Conversions
Convert dates to Date objects, ISO strings, Unix timestamps, arrays, and objects.
To Native Types
Section titled “To Native Types”toDate()
Section titled “toDate()”Convert to a native Date object:
const date = daytime('2025-11-15T14:30:45Z')const nativeDate = date.toDate()
console.log(nativeDate instanceof Date)// true
console.log(nativeDate.getTime())// 1763217045000toString()
Section titled “toString()”Convert to string representation:
const date = daytime('2025-11-15T14:30:45Z')date.toString()// "Sat Nov 15 2025 21:30:45 GMT+0700 (Western Indonesia Time)"valueOf()
Section titled “valueOf()”Get the primitive value (timestamp in milliseconds):
const date = daytime('2025-11-15T14:30:45Z')date.valueOf()// 1763217045000To Standard Formats
Section titled “To Standard Formats”toISO()
Section titled “toISO()”Convert to ISO 8601 string:
const date = daytime('2025-11-15T14:30:45.789Z')date.toISO()// "2025-11-15T14:30:45.789Z"toJSON()
Section titled “toJSON()”Convert to JSON string (ISO 8601 format):
const date = daytime('2025-11-15T14:30:45.789Z')date.toJSON()// "2025-11-15T14:30:45.789Z"
// Works with JSON.stringifyJSON.stringify({ date: daytime('2025-11-15') })// '{"date":"2025-11-15T00:00:00.000Z"}'toUnix()
Section titled “toUnix()”Convert to Unix timestamp (seconds since epoch):
const date = daytime('2025-11-15T14:30:45Z')date.toUnix()// 1763217045To Structured Data
Section titled “To Structured Data”toArray()
Section titled “toArray()”Convert to array of components. Returns values in local timezone:
const date = daytime('2025-11-15T14:30:45.789Z')date.toArray()// [2025, 11, 15, 21, 30, 45, 789]// [year, month, day, hour, minute, second, millisecond]// Note: hour is 21 (local time) not 14 (UTC time)toObject()
Section titled “toObject()”Convert to object with components. Returns values in local timezone:
const date = daytime('2025-11-15T14:30:45.789Z')date.toObject()// {// year: 2025,// month: 11,// day: 15,// hour: 21,// minute: 30,// second: 45,// millisecond: 789// }// Note: hour is 21 (local time, not UTC)Examples
Section titled “Examples”Serialize for API
Section titled “Serialize for API”function serializeForAPI(date: Daytime) { return { iso: date.toISO(), unix: date.toUnix(), components: date.toObject() }}
const data = serializeForAPI(daytime('2025-11-15T14:30:45Z'))// {// iso: "2025-11-15T14:30:45.000Z",// unix: 1763217045,// components: { year: 2025, month: 11, ... }// }Store in Database
Section titled “Store in Database”function prepareForDatabase(date: Daytime) { return { timestamp: date.toUnix(), iso_string: date.toISO(), native_date: date.toDate() }}Extract Components
Section titled “Extract Components”const [year, month, day] = daytime('2025-11-15').toArray().slice(0, 3)console.log(year, month, day)// 2025 11 15
const { hour, minute } = daytime('2025-11-15T14:30:45Z').toObject()console.log(hour, minute)// 21 30Compare with Native Date
Section titled “Compare with Native Date”const daytimeDate = daytime('2025-11-15T14:30:45Z')const nativeDate = daytimeDate.toDate()
// Use native Date methodsconsole.log(nativeDate.getUTCHours())// 14
// Compareconsole.log(daytimeDate.valueOf() === nativeDate.getTime())// true