The 7 days of the week
The 12 months of the year
The Intl name style.
The default calendar where Saturday and Sunday are considered holidays.
The local timezone.
The timezone for UTC.
Return the end of the day for the given date.
import { endOfDay, tzUtc } from '@jetblack/date'
const d1 = tzUtc.makeDate(2000, 1, 1)
const d2 = endOfDay(d1, tzUtc)
console.log(d2.toISOString())
// 2000-02-01T23:59:59.999Z
The start date.
An optional timezone. Defaults to the local timezone.
A new date which is the end of the day.
Return the end of the hour for the given date.
import { endOfHour, tzUtc } from '@jetblack/date'
const d1 = tzUtc.makeDate(2000, 1, 1)
const d2 = endOfHour(d1, tzUtc)
console.log(d2.toISOString())
// 2000-02-01T00:59:59.999Z
The start date.
A new date which is the end of the hour.
Return the end of the minute for the given date.
import { endOfMinute, tzUtc } from '@jetblack/date'
const d1 = tzUtc.makeDate(2000, 1, 1)
const d2 = endOfMinute(d1, tzUtc)
console.log(d2.toISOString())
// 2000-02-01T00:00:59.999Z
The start date.
A new date which is the end of the minute.
Calculate the last moment of the month.
import { endOfMonth, tzUtc } from '@jetblack/date'
const days1 = endOfMonth(new Date('2022-03-25T00:00:00Z'), tzUtc)
console.log(days1.toISOString())
// 2022-03-31T23:59:59.999Z
// Compare with lastDayOfMonth.
const days2 = lastDayOfMonth(new Date('2022-03-25T00:00:00Z'), tzUtc)
console.log(days2.toISOString())
// 2022-03-31T00:00:00.000Z
The start date.
An optional timezone. Defaults to the local timezone.
A date which is the last day of the month for the given year and month.
Find the last moment of the quarter.
A date
An optional timezone. Defaults to the local timezone.
The last moment of the quarter.
Return the end of the second for the given date.
import { endOfSecond, tzUtc } from '@jetblack/date'
const d1 = tzUtc.makeDate(2000, 1, 1)
const d2 = endOfSecond(d1, tzUtc)
console.log(d2.toISOString())
// 2000-02-01T00:00:00.999Z
The start date.
A new date which is the end of the second.
The end of today.
An optional timezone. Defaults to the local timezone.
The end of the current day.
Find the last moment of the week for a given date.
Weeks start on Sunday and end on Saturday.
import { endOfWeek, tzUtc } from '@jetblack/date'
const days1 = endOfWeek(new Date('2022-03-25T00:00:00Z'), tzUtc)
console.log(days1.toISOString())
// 2022-03-26T23:59:59.999Z
// Compare to lastDayOfWeek
const days2 = lastDayOfWeek(new Date('2022-03-25T00:00:00Z'), tzUtc)
console.log(days2.toISOString())
// 2022-03-26T00:00:00.000Z
A date.
An optional timezone. Defaults to the local timezone.
The end of the week.
Find the last moment of the week for a given date and first day of week.
import { endOfWeekday, tzUtc } from '@jetblack/date'
const days1 = endOfWeekday(new Date('2022-03-25T00:00:00Z'), 1, tzUtc)
console.log(days1.toISOString())
// 2022-03-27T23:59:59.999Z
// Compare to lastDayOfWeekday
const days2 = lastDayOfWeekday(new Date('2022-03-25T00:00:00Z'), 1, tzUtc)
console.log(days2.toISOString())
// 2022-03-27T00:00:00.000Z
A date.
The first day of the week where 0 is Sunday.
An optional timezone. Defaults to the local timezone.
The end of the week.
Find the last moment of the year.
import { endOfYear, tzUtc } from '@jetblack/date'
const days1 = endOfYear(new Date('2022-03-25T00:00:00Z'), tzUtc)
console.log(days1.toISOString())
// 2022-12-31T23:59:59.999Z
// Compare with lastDayOfYear.
const days2 = lastDayOfYear(new Date('2022-03-25T00:00:00Z'), tzUtc)
console.log(days2.toISOString())
// 2022-12-31T00:00:00.000Z
A date
An optional timezone. Defaults to the local timezone.
The last moment of the year.
Find the last day of the month.
import { lastDayOfMonth, tzUtc } from '@jetblack/date'
const days2 = lastDayOfMonth(new Date('2022-03-25T00:00:00Z'), tzUtc)
console.log(days2.toISOString())
// 2022-03-31T00:00:00.000Z
// Compare with endOfMonth.
const days1 = endOfMonth(new Date('2022-03-25T00:00:00Z'), tzUtc)
console.log(days1.toISOString())
// 2022-03-31T23:59:59.999Z
The start date.
An optional timezone. Defaults to the local timezone.
A date which is the last day of the month.
Find the last day of the quarter.
import { lastDayOfQuarter, tzLocal } from '@jetblack/date'
console.log(
tzLocal.toISOString(lastDayOfQuarter(tzLocal.makeDate(2000, 1, 2), tzLocal))
)
// returns 2000-03-31T00:00:00-??:??
The start date.
An optional timezone. Defaults to the local timezone.
A date which is the last day of the quarter.
Find the last day of the week for a given date.
Note: weeks start on Sunday and end on Saturday.
import { lastDayOfWeek, tzUtc } from '@jetblack/date'
const days2 = lastDayOfWeek(new Date('2022-03-25T00:00:00Z'), tzUtc)
console.log(days2.toISOString())
// 2022-03-26T00:00:00.000Z
// Compare to endOfWeek
const days1 = endOfWeek(new Date('2022-03-25T00:00:00Z'), tzUtc)
console.log(days1.toISOString())
// 2022-03-26T23:59:59.999Z
A date.
An optional timezone. Defaults to the local timezone.
A date which is the last day of the week.
Find the last day of the week for a given date and first day of week.
import { endOfWeekday, lastDayOfWeekday, tzUtc } from '@jetblack/date'
const days2 = lastDayOfWeekday(new Date('2022-03-25T00:00:00Z'), 1, tzUtc)
console.log(days2.toISOString())
// 2022-03-27T00:00:00.000Z
// Compare to endOfWeekday
const days1 = endOfWeekday(new Date('2022-03-25T00:00:00Z'), 1, tzUtc)
console.log(days1.toISOString())
// 2022-03-27T23:59:59.999Z
A date.
The first day of the week where 0 is Sunday.
An optional timezone. Defaults to the local timezone.
A date which is the last day of the week.
Find the last day of the year.
import { endOfYear, lastDayOfYear, tzUtc } from '@jetblack/date'
const days2 = lastDayOfYear(new Date('2022-03-25T00:00:00Z'), tzUtc)
console.log(days2.toISOString())
// 2022-12-31T00:00:00.000Z
// Compare with endOfYear.
const days1 = endOfYear(new Date('2022-03-25T00:00:00Z'), tzUtc)
console.log(days1.toISOString())
// 2022-12-31T23:59:59.999Z
The start date.
An optional timezone. Defaults to the local timezone.
A date which is the last day of the year.
Find the start of the current month.
An optional timezone. Defaults to the local timezone.
The start of the current month.
Find the start of the current year.
An optional timezone. Defaults to the local timezone.
The start of the current year.
Find the start of the day.
import { startOfDay, tzLocal } from '@jetblack/date'
const date = tzLocal.makeDate(2000, 0, 1, 10, 30)
console.log(tzLocal.toISOString(startOfDay(date, tzLocal)))
// returns "2000-01-01T00:00:00+??:??"
The date.
An optional timezone. Defaults to the local timezone.
The start of the day.
Find the start of the hour.
The date.
The start of the hour.
Find the start of an ISO week for a given date.
ISO weeks start on a Monday.
A date.
An optional timezone. Defaults to the local timezone.
The start of the week.
Find the start of the minute.
The date.
The start of the minute.
Find the start of the month for a given date.
The date.
An optional timezone. Defaults to the local timezone.
The start of the month.
Find the start of the quarter for a given date.
The date.
An optional timezone. Defaults to the local timezone.
The start of the quarter.
Find the start of the second.
The date.
The start of the second.
Find the start of today.
An optional timezone. Defaults to the local timezone.
The start of today.
Find the start of the week for a given date.
A date.
An optional timezone. Defaults to the local timezone.
The start of the week.
Find the first week of the year for the week year of a given date.
The date.
An optional timezone. Defaults to the local timezone.
The date of the first week of the year.
Find the start of the week for a given date and first day of week.
A date.
The first day of the week where 0 is Sunday.
An optional timezone. Defaults to the local timezone.
The start of the week.
Find the start of the year for a given date.
The date.
An optional timezone. Defaults to the local timezone.
The start of the year.
Add business days to a date (or subtract if negative).
If a calendar is not specified the calWeekends
calendar is used.
import { addBusinessDays } from '@jetblack/date'
// Fri 7 Jan 2000
const date = addBusinessDays(new Date('2000-01-07T00:00:00Z'), 1)
console.log(date.getTime() === new Date('2000-01-10T00:00:00Z').getTime())
The start date.
The number of days to add (or subtract if negative).
The calendar to use to identify dates which are holidays. Defaults to the calWeekends
calendar.
An optional timezone. Defaults to the local timezone.
A new date adjusted by business days.
Add days to a date (or subtract if negative)
Daylight savings time (DST) presents an issue for date arithmetic.
The following example adds three days to the Saturday before the clocks change using the local timezone (London) and UTC (which does not have DST).
When using the local timezone the "naturally correct" answer is returned. However, the elapsed time will be an hour less than when using UTC.
Keeping the time change constant (as with UTC) can be useful when plotting data, or doing time series calculations (rolling averages, resampling, etc.).
import { addDays, tzLocal, tzUtc } from '@jetblack/date'
// The local timezone here is London.
// In London on Sunday March 26 2000 the clocks go forward 1 hour.
const d1 = new Date('2000-03-25T12:00:00')
const d2 = addDays(d1, 3, tzLocal)
const d3 = addDays(d1, 3, tzUtc)
console.log(d1.toString()) // Sat Mar 25 2000 12:00:00 GMT+0000 (Greenwich Mean Time)
console.log(d2.toString()) // Tue Mar 28 2000 12:00:00 GMT+0100 (British Summer Time)
console.log(d3.toString()) // Tue Mar 28 2000 13:00:00 GMT+0100 (British Summer Time)
The start date.
The number of days to add (or subtract if negative).
An optional timezone. Defaults to the local timezone.
A new date adjusted by the number of days.
Add a duration to a date.
import { Duration, tzLocal } from '@jetblack/date'
const duration = new Duration('P1DT3H')
const date = addDuration(tzLocal.makeDate(2000, 0, 1), duration, tzLocal)
The start date.
The duration to add.
An optional timezone. Defaults to the local timezone.
A new date adjusted by adding the duration.
Add hours to a date (or subtract if negative).
Daylight savings time is handled appropriately.
import { addHours } from '@jetblack/date'
// The local timezone here is London.
// In London on Sunday March 26 2000 the clocks go forward 1 hour.
const d1 = new Date('2000-03-26T00:00:00')
console.log(d1)
// Sun Mar 26 2000 00:00:00 GMT+0000 (Greenwich Mean Time)
const d2 = addHours(d1, 1)
console.log(d2)
// Sun Mar 26 2000 02:00:00 GMT+0100 (British Summer Time)
The start date.
The number of hours to add (or subtract if negative).
A new date adjusted by the number of hours.
Adds milliseconds to a date (or subtracts if negative).
import { addMilliseconds } from '@jetblack/date'
const d1 = addMilliseconds(new Date('2000-01-01T00:00:00.000'), 111)
console.log(d1.toISOString())
// 2000-01-01T00:00:00.111Z
The start date.
The number of milliseconds to add (or subtract if negative).
A new date adjusted by the number of milliseconds.
Adds minutes to a date (or subtracts if negative).
import { addMinutes } from '@jetblack/date'
const d1 = addMinutes(new Date('2000-01-01T00:00:00.000'), 5)
console.log(d1.toISOString())
// 2000-01-01T00:05:00.000Z
The start date.
The number of minutes to ad (or subtract if negative).
A new date adjusted by the number of minutes.
Add months to a date (or subtract if negative).
The day of the month is kept constant if possible. Where the destination has less days at the end of the month, the surplus days are added.
import { addMonths, tzUtc } from '@jetblack/date'
// The day of the month is kept constant if possible.
const d1 = new Date('2000-01-12')
console.log(d1.toString(), tzUtc)
// Wed Jan 12 2000 00:00:00 GMT+0000 (Greenwich Mean Time)
const d2 = addMonths(d1, 1, tzUtc)
console.log(d2.toString())
// Sat Feb 12 2000 00:00:00 GMT+0000 (Greenwich Mean Time)
// If there are less days in the following month the surplus days are added.
const d3 = new Date('2000-01-31')
console.log(d3.toString())
// Mon Jan 31 2000 00:00:00 GMT+0000 (Greenwich Mean Time)
const d4 = addMonths(d3, 1, tzUtc)
console.log(d4.toString())
// Thu Mar 02 2000 00:00:00 GMT+0000 (Greenwich Mean Time)
The start date.
The number of months to add (or subtract if negative)
An optional timezone. Defaults to the local timezone.
A new date adjusted by the number of months.
Add or subtract days of the week.
If the start date lies on the specified day of the week and the strictly different flag is false, the current date would be considered the first day of the week.
Note that the JavaScript Date object treats 0 as Sunday and 6 as Saturday.
import { addNthDayOfWeek } from '@jetblack/date'
// Saturday 1 January 1990
const d1 = new Date('2000-01-01T00:00:00Z')
// The third Thursday of the month.
const d2 = addNthDayOfWeek(d1, 3, 4, false)
console.log(d2)
// Thu Jan 20 2000 00:00:00 GMT+0000 (Greenwich Mean Time)
// The last Friday of the month
const d3 = addNthDayOfWeek(endOfMonth(d1), -1, 5, false)
console.log(d3)
// Fri Jan 28 2000 00:00:00 GMT+0000 (Greenwich Mean Time)
The start date.
The number of week days.
The day of the week where Sunday is 0.
When true the returned date must be different to the start date.
An optional timezone. Defaults to the local timezone.
The nth week day.
Add quarters to a date (or subtract if negative).
The day of the quarter is kept constant if possible. Where the destination has less days at the end of the month, the surplus days are added.
The start date.
The number of quarters to add (or subtract if negative)
An optional timezone. Defaults to the local timezone.
A new date adjusted by the number of quarters.
Adds seconds to a date (or subtracts if negative).
import { addSeconds } from '@jetblack/date'
const d1 = addSeconds(new Date('2000-01-01T00:00:00.000'), 61)
console.log(d1.toISOString())
// 2000-01-01T00:01:01.000Z
The start date.
The number of seconds to add.
A new date adjusted by the number of seconds.
Add years to a date (or subtract if negative).
Note how surplus days for a leap year are added.
import { addYears, tzUtc } from '@jetblack/date'
// There is no 29 February in 2001.
const d1 = addYears(new Date('2000-02-29'), 1, tzUtc)
console.log(d1.toString())
// Thu Mar 01 2001 00:00:00 GMT+0000 (Greenwich Mean Time)
// 2004 is also a leap year.
const d2 = addYears(new Date('2000-02-29'), 4, tzUtc)
console.log(d2.toString())
// Sun Feb 29 2004 00:00:00 GMT+0000 (Greenwich Mean Time)
The start date.
The number of years to add (or subtract if negative).
An optional timezone. Defaults to the local timezone.
A new date adjusted by the number of years.
Find the number of days in a month for a given year.
import { daysInMonth } from '@jetblack/date'
console.log(daysInMonth(2000, 0))
// 31
console.log(daysInMonth(2000, 1))
// 29
console.log(daysInMonth(2001, 1))
// 28
The year.
The month index where January is 0.
The number of days in the month for the given year.
Find the number of days in a year.
import { daysInYear } from '@jetblack/date'
console.log(daysInYear(2000))
// 366
console.log(daysInYear(2001))
// 365
The year.
The number of days in the year.
Calculates Easter in the Gregorian calendar.
The year.
An optional timezone. Defaults to the local timezone.
The date for easter in the given year.
Check if the date is the last day of the month.
The time component is ignored.
The date to check.
An optional timezone. Defaults to the local timezone.
True if the date is the last day of the month.
Check if the date is the last day of the quarter.
Any time component is ignored.
import { tzLocal, isLastDayOfQuarter } from '@jetblack/date'
const jan30 = tzLocal.makeDate(2008, 0, 30)
console.log(isLastDayOfQuarter(jan30, tzLocal))
// returns false
const mar31 = tzLocal.makeDate(2008, 2, 31)
console.log(isLastDayOfQuarter(mar31, tzLocal))
// returns true
The date to check.
An optional timezone. Defaults to the local timezone.
True if the date is the last day of the quarter.
Check if the year is a leap year.
import { isLeapYear } from '@jetblack/date'
// 2000 was a century leap year.
console.log(isLeapYear(2000) === true)
// 1900 was not.
console.log(isLeapYear(1900) !== true)
The year.
True if the year is a leap year, otherwise false.
Find the quarter for a given date.
import { quarterOfYear } from '@jetblack/date'
console.log(quarterOfYear(new Date('2000-04-01')))
// 2
The date.
An optional timezone. Defaults to the local timezone.
The quarter of the year.
Compare two dates to see if they are in the same quarter of the year.
import { areInSameQuarter } from '@jetblack/date'
console.log(areInSameQuarter(new Date('2000-01-01'), new Date('2000-02-01')))
// true
console.log(areInSameQuarter(new Date('2000-01-01'), new Date('2000-04-01')))
// false
The first date.
The second date.
An optional timezone. Defaults to the local timezone.
True if the dates are in the same quarter, otherwise false.
Compares two dates ascending.
import { compareDateAsc } from '@jetblack/date'
const a = [
new Date('2000-07-01'),
new Date('2000-05-01'),
new Date('2000-06-01')
]
console.log(a.sort(compareDateAsc))
// 0: Mon May 01 2000 01:00:00 GMT+0100 (British Summer Time)
// 1: Thu Jun 01 2000 01:00:00 GMT+0100 (British Summer Time)
// 2: Sat Jul 01 2000 01:00:00 GMT+0100 (British Summer Time)
The first date.
The second date.
1 if the first date is greater than the second date, -1 if the first date is less than the last date, otherwise 0.
Compares two dates descending.
import { compareDateDesc } from '@jetblack/date'
const a = [
new Date('2000-07-01'),
new Date('2000-05-01'),
new Date('2000-06-01')
]
console.log(a.sort(compareDateDesc))
// 0: Sat Jul 01 2000 01:00:00 GMT+0100 (British Summer Time)
// 1: Thu Jun 01 2000 01:00:00 GMT+0100 (British Summer Time)
// 2: Mon May 01 2000 01:00:00 GMT+0100 (British Summer Time)
The first date.
The second date.
1 if the second date is greater than the first date, -1 if the second date is less than the first date, otherwise 0.
Checks if the left date is after the right date.
import { isDateAfter } from '@jetblack/date'
console.log(isDateAfter(new Date('2000-01-02'), new Date('2000-01-01')))
// returns true
console.log(isDateAfter(new Date('2000-01-01'), new Date('2000-01-02')))
// returns false
console.log(isDateAfter(new Date('2000-01-01'), new Date('2000-01-01')))
// returns false
The left date.
The right date.
True if the left date is after the right date, otherwise false.
Checks if the dates are not equal.
import { isDateNotEqual } from '@jetblack/date'
console.log(isDateNotEqual(new Date('2000-01-02'), new Date('2000-01-01')))
// returns true
console.log(isDateNotEqual(new Date('2000-01-01'), new Date('2000-01-02')))
// returns true
console.log(isDateNotEqual(new Date('2000-01-01'), new Date('2000-01-01')))
// returns false
The left date.
The right date.
True if the dates are not equal, otherwise false.
Checks if the left date is on or after the right date.
import { isDateOnOrAfter } from '@jetblack/date'
console.log(isDateOnOrAfter(new Date('2000-01-02'), new Date('2000-01-01')))
// returns true
console.log(isDateOnOrAfter(new Date('2000-01-01'), new Date('2000-01-02')))
// returns false
console.log(isDateOnOrAfter(new Date('2000-01-01'), new Date('2000-01-01')))
// returns true
The left date.
The right date.
True if the left date is on or after the right date, otherwise false.
Checks if the left date is on or before the right date.
import { isDateOnOrBefore } from '@jetblack/date'
console.log(isDateOnOrBefore(new Date('2000-01-02'), new Date('2000-01-01')))
// returns false
console.log(isDateOnOrBefore(new Date('2000-01-01'), new Date('2000-01-02')))
// returns true
console.log(isDateOnOrBefore(new Date('2000-01-01'), new Date('2000-01-01')))
// returns true
The left date.
The right date.
True if the left date is on or before the right date, otherwise false.
Find the largest date.
import { maxDate } from '@jetblack/date'
const d1 = maxDate(
new Date('2000-01-31T00:00:00Z'),
new Date('2000-01-01T00:00:00Z'),
new Date('2000-01-10T00:00:00Z')
)
console.log(d1.toISOString())
// 2000-01-31T00:00:00.000Z
const dates = [
new Date('2000-01-31T00:00:00Z'),
new Date('2000-01-01T00:00:00Z'),
new Date('2000-01-10T00:00:00Z')
]
const d2 = maxDate(...dates) // use the spread operator for an array.
console.log(d2.toISOString())
// 2000-01-31T00:00:00.000Z
The dates to check.
The largest date.
Find the smallest date.
import { minDate } from '@jetblack/date'
const d1 = minDate(
new Date('2000-01-31T00:00:00Z'),
new Date('2000-01-01T00:00:00Z'),
new Date('2000-01-10T00:00:00Z')
)
console.log(d1.toISOString())
// 2000-01-01T00:00:00.000Z
const dates = [
new Date('2000-01-31T00:00:00Z'),
new Date('2000-01-01T00:00:00Z'),
new Date('2000-01-10T00:00:00Z')
]
const d2 = minDate(...dates) // use the spread operator for an array
console.log(d2.toISOString())
// 2000-01-01T00:00:00.000Z
The dates to check.
The smallest date.
Checks if the left date is before the right date.
The left date.
The right date.
True if the left date is before the right date, otherwise false.
Checks if the dates are equal.
The left date.
The right date.
True if the left date equals the right date, otherwise false.
Find the number of whole days between two dates, discarding any time component.
import { diffInCalDays, tzUtc } from '@jetblack/date'
const days1 = diffInCalDays(
new Date('2022-03-25T00:00:00Z'),
new Date('2022-01-25T00:00:00Z'),
tzUtc
)
console.log(days1)
// 59
// The time part is discarded.
const days2 = diffInCalDays(
new Date('2022-03-25T12:00:00Z'),
new Date('2022-01-25T00:00:00Z'),
tzUtc
)
console.log(days2)
// 59
The left date.
The right date.
An optional timezone. Defaults to the local timezone.
The number of days between the start of day on the start and end dates.
Find the number of whole months between two dates.
The left date.
The right date.
An optional timezone. Defaults to the local timezone.
The number of months between two dates.
Find the number of whole years between two dates.
The left date.
The right date.
An optional timezone. Defaults to the local timezone.
The number of years between two dates.
Find the number of days between two dates including the fractional component.
import { diffInDays, tzUtc } from '@jetblack/date'
const days1 = diffInDays(
new Date('2022-03-25T00:00:00Z'),
new Date('2022-01-25T00:00:00Z'),
tzUtc
)
console.log(days1)
// 59
// The time part is retained.
const days2 = diffInDays(
new Date('2022-03-25T12:00:00Z'),
new Date('2022-01-25T00:00:00Z'),
tzUtc
)
console.log(days2)
// 59.5
The left date.
The right date.
The number of days between the start of day on the start and end dates.
Find the number of hours between two dates including fractional minutes.
The left date.
The right date.
The number of days between the start of day on the start and end dates.
Find the number of milliseconds between two dates.
The left date.
The right date.
The number of days milliseconds between the left and right date.
Find the number of minutes between two dates including fractional seconds.
The left date.
The right date.
The number of days between the start of day on the start and end dates.
Find the number of seconds between two dates including fractional milliseconds.
The left date.
The right date.
The number of days between the start of day on the start and end dates.
Format a date with a pattern.
import { formatDate } from '@jetblack/date'
const d = new Date("2000-01-01")
const s = formatDate(d, "d-mmm-yy")
console.log(s)
> 1-Jan-00
Value | Description |
---|---|
d | Day of the month as digits; no leading zero for single-digit days. |
dd | Day of the month as digits; leading zero for single-digit days. |
ddd | Day of the week as the short representation (for en a three-letter abbreviation). |
dddd | Day of the week as its full name. |
DD | Day of the month with the plural suffix. |
DDD | Day of the week as the narrow representation. |
m | Month as digits; no leading zero for single-digit months. |
mm | Month as digits; leading zero for single-digit months. |
mmm | Month as a three-letter abbreviation. |
mmmm | Month as its full name. |
yy | Year as last two digits; leading zero for years less than 10. |
yyyy | Year represented by four digits. |
h | Hours; no leading zero for single-digit hours (12-hour clock). |
hh | Hours; leading zero for single-digit hours (12-hour clock). |
H | Hours; no leading zero for single-digit hours (24-hour clock). |
HH | Hours; leading zero for single-digit hours (24-hour clock). |
M | Minutes; no leading zero for single-digit minutes. |
MM | Minutes; leading zero for single-digit minutes. |
S | Seconds; no leading zero for single-digit seconds. |
SS | Seconds; leading zero for single-digit seconds. |
F | Hundreds of milliseconds. |
FF | Tens of milliseconds with a leading zero for single-digit values. |
FFF | Milliseconds: zero padded. |
t | Narrow dayPeriod from locale. |
tt | Short dayPeriod from locale. |
ttt | Long dayPeriod from locale. |
Z | Timezone name. |
o | GMT/UTC timezone offset, e.g. -0500 or +0230. |
'...' or "..." | Literal character sequence. Surrounding quotes are removed. |
A date.
The format pattern.
An optional timezone. Defaults to the local timezone.
An optional locale. Defaults to the current browser locale.
The formatted date string.
Parse a date string into a Javascript Date object.
If the timezone offset is absent the dates will be according to the local timezone.
import { parseDate } from '@jetblack/date'
const d = parseDate('12 March, 1998', 'dd mmm, yyyy')
Value | Description |
---|---|
d | Day of the month as digits; no leading zero for single-digit days. |
dd | Day of the month as digits; leading zero for single-digit days. |
ddd | Day of the week as a three-letter abbreviation. |
dddd | Day of the week as its full name. |
DD | Day of the month with the plural suffix. |
m | Month as digits; no leading zero for single-digit months. |
mm | Month as digits; leading zero for single-digit months. |
mmm | Month as a three-letter abbreviation. |
mmmm | Month as its full name. |
yy | Year as last two digits; leading zero for years less than 10. |
yyyy | Year represented by four digits. |
h | Hours; no leading zero for single-digit hours (12-hour clock). |
hh | Hours; leading zero for single-digit hours (12-hour clock). |
H | Hours; no leading zero for single-digit hours (24-hour clock). |
HH | Hours; leading zero for single-digit hours (24-hour clock). |
M | Minutes; no leading zero for single-digit minutes. |
MM | Minutes; leading zero for single-digit minutes. |
S | Seconds; no leading zero for single-digit seconds. |
SS | Seconds; leading zero for single-digit seconds. |
F | Hundreds of milliseconds. |
FF | Tens of milliseconds with a leading zero for single-digit values. |
FFF | Milliseconds: zero padded. |
t | Narrow dayPeriod from locale. |
tt | Short dayPeriod from locale. |
ttt | Long dayPeriod from locale. |
Z | Timezone offset. |
[dhM] | Literal characters. |
Date string
Date parse format
An optional timezone. Defaults to the local timezone.
An optional locale. Defaults to the current locale.
The date, or null if parsing failed.
Find the nearest business date.
The start date.
If true a future date is preferred if both directions have the same cost. Defaults to true.
The calendar to use to identify business days. Defaults to the weekend calendar.
An optional timezone. Defaults to the local timezone.
The nearest business date.
Find the week of the year for a given date.
Weeks start on 1 January of any given year.
import { weekOfYear, tzUtc } from '@jetblack/date'
console.log(weekOfYear(new Date('1999-12-31T00:00:00Z'), tzUtc))
// 53
console.log(weekOfYear(new Date('2000-01-01T00:00:00Z'), tzUtc))
// 1
console.log(weekOfYear(new Date('2000-01-07T00:00:00Z'), tzUtc))
// 1
console.log(weekOfYear(new Date('2000-01-08T00:00:00Z'), tzUtc))
// 2
console.log(weekOfYear(new Date('2000-12-31T00:00:00Z'), tzUtc))
// 53
The date.
An optional timezone. Defaults to the local timezone.
The week of the year.
Find the week year for a given date.
The date.
An optional timezone. Defaults to the local timezone.
The week year.
Adjusts a non-business day to the appropriate nearest business day.
import { adjustBusinessDay, BusinessDayConvention } from '@jetblack/date'
const d1 = new Date('2000-01-01')
console.log(d1)
// Sat Jan 01 2000 00:00:00 GMT+0000 (Greenwich Mean Time)
const d2 = adjustBusinessDay(d1, BusinessDayConvention.FOLLOWING)
console.log(d2.toString())
// Mon Jan 03 2000 00:00:00 GMT+0000 (Greenwich Mean Time)
const d3 = adjustBusinessDay(d1, BusinessDayConvention.PRECEDING)
console.log(d3.toString())
// Fri Dec 31 1999 00:00:00 GMT+0000 (Greenwich Mean Time)
The date.
The business day conventions. Defaults to BusinessDayConvention.FOLLOWING.
If true prefer the nearest business day in the future. Defaults to true.
An optional holiday calendar. Defaults to calWeekend.
An optional timezone. Defaults to the local timezone.
The adjusted date.
Find the day of the year.
import { dayOfYear, tzUtc } from '@jetblack/date'
const d = new Date('2020-07-12T00:00:00Z')
const day = dayOfYear(d, tzUtc)
console.log(day)
// 94
The date
An optional timezone. Defaults to the local timezone.
The day of the year
Find the index of the first date with the given day of the month.
The dates
The day of the month to check
An optional timezone. Defaults to the local timezone.
The index of the first match, or -1.
Find the index of the first date that is the end of the month.
The dates
An optional timezone. Defaults to the local timezone.
The index of the first match, or -1.
Find the index of the last date with the given day of the month.
The dates
The day of the month to check
An optional timezone. Defaults to the local timezone.
The index of the last match, or -1.
Find the index of the last date that is the end of the month.
The dates
An optional timezone. Defaults to the local timezone.
The index of the last match, or -1.
Find the index of the last date that is the start of the month.
The dates
An optional timezone. Defaults to the local timezone.
The index of the last match, or -1.
Find the index of the last date with the given weekday.
The dates
The weekday to check
An optional timezone. Defaults to the local timezone.
The index of the last match, or -1.
Find the index of the first date that is the start of the month.
The dates
An optional timezone. Defaults to the local timezone.
The index of the first match, or -1.
Find the index of the first date with the given weekday.
The dates
The weekday to check
An optional timezone. Defaults to the local timezone.
The index of the first match, or -1.
Get the locale information.
A populated LocalInfo
Check if a value is a valid date.
import { isValidDate } from '@jetblack/date'
> const value = new Date("foo")
> console.log(isValidDate(value))
false
The value to check.
True if the value is a valid date.
Find the ISO week date.
import { isoWeekDate } from '@jetblack/date'
console.log(isoWeekDate(new Date("2000-01-01T00:00:00Z")))
// [1999, 52, 6]
The date
An optional timezone. Defaults to the local timezone.
The ISO date as a year, week, and day.
Find the ISO week of the year.
The date.
An optional timezone. Defaults to the local timezone.
The ISO week.
Find the number of leap seconds between the dates (inclusive).
import { endOfYear, leapSeconds, tzUtc } from '@jetblack/date'
const d1 = new Date('1972-01-01T00:00:00Z')
const d2 = endOfYear(d1, tzUtc)
// There were two leap seconds in 1972
console.log(leapSeconds(d1, d2) == 2)
const d3 = new Date('2000-01-01T00:00:00Z')
const d4 = endOfYear(new Date('2005-01-01T00:00:00Z'), tzUtc)
// There were no leap seconds in between 2000 and 2005
console.log(leapSeconds(d3, d4) == 0)
The first date.
The second date.
The number of leap seconds
Round a date to the start of the current day if before noon; otherwise the start of the next day.
import { roundDate, tzLocal } from '@jetblack/date'
const morning = tzLocal.makeDate(2000, 0, 1, 10, 30)
console.log(tzLocal.toISOString(roundDate(morning, tzLocal)))
// returns "2000-01-01T00:00:00+??:??"
const afternoon = tzLocal.makeDate(2000, 0, 1, 18, 30)
console.log(tzLocal.toISOString(roundDate(afternoon, tzLocal)))
// returns "2000-01-02T00:00:00+??:??"
const noon = tzLocal.makeDate(2000, 0, 1, 12, 0)
console.log(tzLocal.toISOString(roundDate(noon, tzLocal)))
// returns "2000-01-02T00:00:00+??:??"
The date to round
An optional timezone. Defaults to the local timezone.
The start of the day of the rounded date.
Creates a range of dates by day.
import { dateRangeByDay, tzUtc } from '@jetblack/date'
const dates = dateRangeByDay(
new Date('2000-01-01T12:00:00Z'), // Sets to start of day.
new Date('2000-01-10T12:00:00Z'),
1,
tzUtc
)
dates.forEach(date => console.log(date.toISOString()))
// 2000-01-01T00:00:00.000Z
// 2000-01-02T00:00:00.000Z
// 2000-01-03T00:00:00.000Z
// 2000-01-04T00:00:00.000Z
// 2000-01-05T00:00:00.000Z
// 2000-01-06T00:00:00.000Z
// 2000-01-07T00:00:00.000Z
// 2000-01-08T00:00:00.000Z
// 2000-01-09T00:00:00.000Z
// 2000-01-10T00:00:00.000Z
The start date.
The end date.
The day step count.
An optional timezone. Defaults to the local timezone.
The range of dates separated by step days from the start to the end date.
Creates a range of dates by hours.
The start date.
The end date.
The hour step count.
The range of dates separated by step hours from the start to the end hour.
Creates a range of dates by minutes.
The start date.
The end date.
The minute step count.
The range of dates separated by step hours from the start to the end minute.
Creates a range of dates by month.
The start date.
The end date.
The month step count.
An optional timezone. Defaults to the local timezone.
The range of dates separated by step months from the start to the end date.
Creates a range of dates by quarters.
The start date.
The end date.
The quarter step count. Defaults to 1.
An optional timezone. Defaults to the local timezone.
The range of dates separated by step months from the start to the end date.
Creates a range of dates by seconds.
The start date.
The end date.
The second step count.
The range of dates separated by step hours from the start to the end second.
Creates a range of dates by week.
The start date.
The end date.
The week step count.
An optional timezone. Defaults to the local timezone.
The range of dates separated by step weeks from the start to the end date.
Creates a range of dates by year.
The start date.
The end date.
The year step count.
An optional timezone. Defaults to the local timezone.
The range of dates separated by step years from the start to the end date.
Creates a schedule of dates for a periodicity.
The periodicity.
The start date.
Start offset periods.
The number of periods.
The period step count.
An optional timezone. Defaults to the local timezone.
The schedule of dates.
Creates a schedule of dates by day.
import { dateScheduleByDay, tzLocal } from '@jetblack/date'
console.log(
dateScheduleByDay(tzLocal.makeDate(2000, 0, 1), 0, 7).map(x =>
tzLocal.toISOString(x)
)
)
// returns (7) ['2000-01-01T00:00:00+??:??', '2000-01-02T00:00:00+??:??', '2000-01-03T00:00:00+??:??', '2000-01-04T00:00:00+??:??', '2000-01-05T00:00:00+??:??', '2000-01-06T00:00:00+??:??', '2000-01-07T00:00:00+??:??']
The start date.
Start offset days.
The number of days.
The days step count.
An optional timezone. Defaults to the local timezone.
The schedule of dates separated by step days.
Creates a schedule of dates by month.
import { dateScheduleByMonth, tzLocal } from '@jetblack/date'
console.log(
dateScheduleByMonth(tzLocal.makeDate(2000, 0, 1), 0, 6).map(x =>
tzLocal.toISOString(x)
)
)
// returns (6) ['2000-01-01T00:00:00+??:??', '2000-02-01T00:00:00+??:??', '2000-03-01T00:00:00+??:??', '2000-04-01T00:00:00+??:??', '2000-05-01T00:00:00+??:??', '2000-06-01T00:00:00+??:??']
The start date.
Start offset months.
The number of months.
The months step count.
An optional timezone. Defaults to the local timezone.
The schedule of dates separated by step months.
Creates a schedule of dates by quarter.
import { dateScheduleByQuarter, tzLocal } from '@jetblack/date'
console.log(
dateScheduleByQuarter(tzLocal.makeDate(2000, 0, 1), 0, 4).map(x =>
tzLocal.toISOString(x)
)
)
// returns (4) ['2000-01-01T00:00:00+??:??', '2000-04-01T00:00:00+??:??', '2000-07-01T00:00:00+??:??', '2000-10-01T00:00:00+??:??']
The start date.
Start offset quarters.
The number of quarters.
The quarters step count.
An optional timezone. Defaults to the local timezone.
The schedule of dates separated by step quarter.
Creates a schedule of dates by week.
import { dateScheduleByWeek, tzLocal } from '@jetblack/date'
console.log(
dateScheduleByWeek(tzLocal.makeDate(2000, 0, 1), 0, 4).map(x =>
tzLocal.toISOString(x)
)
)
// returns (4) ['2000-01-01T00:00:00+??:??', '2000-01-08T00:00:00+??:??', '2000-01-15T00:00:00+??:??', '2000-01-22T00:00:00+??:??']
The start date.
Start offset weeks.
The number of weeks.
The weeks step count.
An optional timezone. Defaults to the local timezone.
The schedule of dates separated by step weeks.
Creates a schedule of dates by year.
import { dateScheduleByYear, tzLocal } from '@jetblack/date'
console.log(
dateScheduleByYear(tzLocal.makeDate(2000, 0, 1), 0, 4).map(x =>
tzLocal.toISOString(x)
)
)
// returns (4) ['2000-01-01T00:00:00+??:??', '2001-01-01T00:00:00+??:??', '2002-01-01T00:00:00+??:??', '2003-01-01T00:00:00+??:??']
The start date.
Start offset years.
The number of years.
The years step count.
An optional timezone. Defaults to the local timezone.
The schedule of dates separated by step years.
Transform the values of a timezone offset.
The object to transform.
The transformed object.
Fetch a timezone.
import { fetchTimezone } from '@jetblack/date'
const tzChicago = await fetchTimezone('America/Chicago')
console.log(tzChicago.makeDate(2022, 12, 25).toISOString())
// 2023-01-25T06:00:00.000Z
The timezone name.
The database version.
The root url.
Fetch options
A promise resolving to the timezone.
Fetch a list of timezone names.
import { fetchTimezoneNames } from '@jetblack/map'
const names = await fetchTimezoneNames()
console.log(names.filter(name => name.startsWith('Arctic')))
// (1) ['Arctic/Longyearbyen']
The database version.
The root url.
Fetch options
A promise resolving to the list of time zone names.
Transform the values of a minified timezone offset.
The object to transform.
The transformed object.
Create a timezone from JSON data.
The JSON data requires transformation to convert date and duration strings to objects.
The timezone name.
The JSON timezone data.
The new timezone.
A JSON.parse reviver for tzdata.
The JSON key.
The JSON value.
A revived value if applicable.
Generated using TypeDoc
Day periods: morning, afternoon.