date

Clamps the given date to the given date range.

Type signature

(min: Date, max: Date) => (date: Date) => Date

Examples

const date = new Date(
  "2019-06-15T13:54:33.232Z",
);
const min = new Date(
  "2019-02-23T13:54:33.232Z",
);
const max = new Date(
  "2019-03-13T13:54:33.232Z",
);

clamp(min, max)(date);
// => new Date("2019-03-13T13:54:33.232Z")
Try in REPL

Questions

  • How to clamp a date to the desired date range?
  • How to enforce a date to be in a given date range?

TypeScript sourceJavaScript source

Clones the given Date object.

Type signature

(date: Date) => Date

Examples

const date = new new Date(
  "2019-04-24T13:54:33.232Z",
)();
const cloned = clone(date);

cloned !== date &&
  cloned.valueOf() ===
    date.valueOf();
// ⇒ true
Try in REPL

Questions

  • How to clone a Date object?

TypeScript sourceJavaScript source

Computes a signed difference between two Date objects as milliseconds.

Type signature

(a: Date, b: Date) => number

Examples

dateDiff(
  new Date(
    "2017-01-01T13:00:00.000Z",
  ),
  new Date(
    "2017-01-01T12:00:00.000Z",
  ),
);
// ⇒ 3600000
Try in REPL

Questions

  • How to compute Date difference?

TypeScript sourceJavaScript source

Checks if the given date is between the given date range (inclusive).

Type signature

(from: Date, to: Date) => (date: Date) => boolean

Examples

dateInRange(
  new Date(
    "2018-06-10T12:00:00.000Z",
  ),
  new Date(
    "2018-06-20T12:00:00.000Z",
  ),
)(
  new Date(
    "2018-06-15T12:00:00.000Z",
  ),
);
// ⇒ true
Try in REPL

Questions

  • How to check if a date is within a given date range?

TypeScript sourceJavaScript source

Returns a local day range at a particular Date.

Type signature

(date: Date) => Date[]

Examples

const date = new Date(
  "2018-12-31T13:54:33.232Z",
);

dayRange(date);
// ⇒ [startOfDay(date), endOfDay(date)]
Try in REPL

Questions

  • How to find a date range of a given day?

TypeScript sourceJavaScript source

Returns an array of days in a particular months. Number of days in February varies if it is a leap year or not.

Type signature

(leapYear: boolean) => [
    number,
    number,
    number,
    number,
    number,
    number,
    number,
    number,
    number,
    number,
    number,
    number
]

Examples

daysInMonths(false);
// ⇒ [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Try in REPL
daysInMonths(true);
// ⇒ [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Try in REPL

Questions

  • How to find out how many days are in a particular month?
  • How to find out how many days there are in a leap year?

TypeScript sourceJavaScript source

Calculates the number of days in a particular year. Varies by the leap year.

Type signature

(year: number) => 366 | 365

Examples

daysInYear(2019);
// ⇒ 365
Try in REPL
daysInYear(2020);
// ⇒ 366
Try in REPL

Questions

  • How many days are in a particular year?
  • How many days are in a leap year?
  • How many days are in a common year?

TypeScript sourceJavaScript source

Displays padded time string.

Type signature

(source: [number, number, number], showSeconds: boolean) => string

Examples

displayTime([5, 12, 16], false);
// ⇒ 05:12
Try in REPL
displayTime([5, 12, 16], true);
// ⇒ 05:12:16
Try in REPL

Questions

  • How to display padded time?

TypeScript sourceJavaScript source

Returns a local Date of an end of the day at a particular Date.

Type signature

(date: Date) => Date

Examples

endOfDay(
  new Date(
    "2018-12-31T13:54:33.232Z",
  ),
);
// ⇒ new Date(new Date("2019-01-01T00:00:00.000Z").valueOf() + new Date("2018-12-31T13:54:33.232Z").getTimezoneOffset() * 60 * 1000)
Try in REPL

Questions

  • How to find a date of an end of a given day?

TypeScript sourceJavaScript source

Formats a given date as a simple YYYY-MM-DD string.

Type signature

(date: Date) => string

Examples

formatDate(
  new Date("2019-02-24T01:12:34"),
);
// ⇒ "2019-02-24"
Try in REPL

Questions

  • How to render a date in a YYYY-MM-DD format?

TypeScript sourceJavaScript source

Formats a given date as a simple YYYY-MM-DD HH:MM(:SS) string.

Type signature

(sourceDate: Date, showSeconds?: boolean) => string

Examples

formatDateTime(
  new Date("2019-02-24T01:12:34"),
);
// ⇒ "2019-02-24 01:12"
Try in REPL
formatDateTime(
  new Date("2019-02-24T01:12:34"),
  true,
);
// ⇒ "2019-02-24 01:12:34"
Try in REPL

Questions

  • How to render a date in a YYYY-MM-DD HH:MM format?
  • How to render a date in a YYYY-MM-DD HH:MM:SS format?

TypeScript sourceJavaScript source

Formats a duration in milliseconds to a padded time string.

Type signature

(duration: number, showSeconds?: boolean) => string

Examples

formatDuration(26100000);
// ⇒ 07:15
Try in REPL
formatDuration(26136000, true);
// ⇒ 07:15:36
Try in REPL

Questions

  • How to render a formatted duration?

TypeScript sourceJavaScript source

Formats a given date as a simple HH:MM(:SS) string.

Type signature

(date: Date, showSeconds?: boolean) => string

Examples

formatTime(
  new Date("2019-02-24T01:12:34"),
);
// ⇒ "01:12"
Try in REPL
formatTime(
  new Date("2019-02-24T01:12:34"),
  true,
);
// ⇒ "01:12:34"
Try in REPL

Questions

  • How to render a date in a HH:MM format?
  • How to render a date in a HH:MM:SS format?

TypeScript sourceJavaScript source

Converts the given day count to milliseconds.

Type signature

(days: number) => number

Examples

fromDays(1);
// ⇒ 86400000
Try in REPL

Questions

  • How to find how many milliseconds are in a given number of days?

TypeScript sourceJavaScript source

Converts the given hour count to milliseconds.

Type signature

(hours: number) => number

Examples

fromHours(1);
// ⇒ 3600000
Try in REPL

Questions

  • How to find how many milliseconds are in a given number of hours?

TypeScript sourceJavaScript source

Converts the given minute count to milliseconds.

Type signature

(minutes: number) => number

Examples

fromMinutes(1);
// ⇒ 60000
Try in REPL

Questions

  • How to find how many milliseconds are in a given number of minutes?

TypeScript sourceJavaScript source

Converts the given second count to milliseconds.

Type signature

(seconds: number) => number

Examples

fromSeconds(1);
// ⇒ 1000
Try in REPL

Questions

  • How to find how many milliseconds are in a given number of seconds?

TypeScript sourceJavaScript source

Joins a date-time pair into a date-time string.

Type signature

(date: string, time: string) => string

Examples

joinDateTime(
  "2019-01-15",
  "13:54:33.232Z",
);
// ⇒ "2019-01-15T13:54:33.232Z"
Try in REPL

Questions

  • How to join date and time to get ISO-compliant date-time string?

TypeScript sourceJavaScript source

Detects if a given year is a leap year.

Type signature

(year: number) => boolean

Examples

leapYear(2020);
// ⇒ true
Try in REPL
leapYear(2019);
// ⇒ false
Try in REPL

Questions

  • How to find if the given year is a leap year?

TypeScript sourceJavaScript source

Parses HH:MM string into hours and minutes.

Type signature

(text?: string) => [number, number]

Examples

parseHourMinutePair("12:34");
// ⇒ [12, 34]
Try in REPL

Questions

  • How to parse time string into hours and minutes?

TypeScript sourceJavaScript source

Splits a date-time string into a date-time pair.

Type signature

(dateTimeString: string) => [string, string]

Examples

splitDateTime(
  "2019-01-15T13:54:33.232Z",
);
// ⇒ ["2019-01-15", "13:54:33.232Z"]
Try in REPL

Questions

  • How to split ISO-compliant date-time string into a date and time pair?

TypeScript sourceJavaScript source

Returns a local Date of a start of the day at a particular Date.

Type signature

(date: Date) => Date

Examples

endOfDay(
  new Date(
    "2019-01-01T13:54:33.232Z",
  ),
);
// ⇒ new Date(new Date("2019-01-01T00:00:00.000Z").valueOf() + new Date("2019-01-01T13:54:33.232Z").getTimezoneOffset() * 60 * 1000)
Try in REPL

Questions

  • How to find a date of the start of a given day?

TypeScript sourceJavaScript source

Subtracts the given number of days from the given Date object.

Type signature

(sourceDate: Date, numberOfDays: number) => Date

Examples

subtractDays(
  new Date(
    "2019-01-15T13:54:33.232Z",
  ),
  1,
);
// ⇒ new Date("2019-01-14T13:54:33.232Z")
Try in REPL

Questions

  • How to subtract days from a given date?

TypeScript sourceJavaScript source

Extracts padded YYYY-MM-DD date string out of the given date object.

Type signature

(date: Date) => string

Examples

toDate(
  new Date(
    "2019-01-15T12:00:00.000Z",
  ),
);
// ⇒ "2019-01-15"
Try in REPL

Questions

  • How to get only the date from a Date object?

TypeScript sourceJavaScript source

Converts the given array of values into Dates using the Date constructor.

Type signature

(xs: (string | number | Date)[]) => Date[]

Examples

toDates([
  "2019-01-15T13:54:33.232Z",
  new Date(
    "2019-01-15T13:54:33.232Z",
  ).valueOf(),
  new Date(
    "2019-01-15T13:54:33.232Z",
  ),
]);
// ⇒ [new Date("2019-01-15T13:54:33.232Z"), new Date("2019-01-15T13:54:33.232Z"), new Date("2019-01-15T13:54:33.232Z")]
Try in REPL

Questions

  • How to convert an array of string and timestamps into an array of Date objects?

TypeScript sourceJavaScript source

Converts milliseconds into days.

Type signature

(milliseconds: number) => number

Examples

toDays(86400000);
// ⇒ 1
Try in REPL

Questions

  • How to convert milliseconds into days?

TypeScript sourceJavaScript source

Converts milliseconds into hours.

Type signature

(milliseconds: number) => number

Examples

toHours(3600000);
// ⇒ 1
Try in REPL

Questions

  • How to convert milliseconds into hours?

TypeScript sourceJavaScript source

Returns an ISO-compliant date-time string.

Type signature

(x: Date) => string

Examples

toISO(
  new Date(
    "2019-04-24T13:54:33.232Z",
  ),
);
// ⇒ "2019-04-24T13:54:33.232Z"
Try in REPL

Questions

  • How to convert Date object to ISO-compliant date string?

TypeScript sourceJavaScript source

Converts milliseconds into minutes.

Type signature

(milliseconds: number) => number

Examples

toMinutes(60000);
// ⇒ 1
Try in REPL

Questions

  • How to convert milliseconds into minutes?

TypeScript sourceJavaScript source

Converts milliseconds into seconds.

Type signature

(milliseconds: number) => number

Examples

toSeconds(1000);
// ⇒ 1
Try in REPL

Questions

  • How to convert milliseconds into seconds?

TypeScript sourceJavaScript source

Checks if the given date is present and it is valid.

Type signature

(date?: unknown) => boolean

Examples

valid(
  new Date(
    "2020-01-31T09:52:31.618Z",
  ),
);
// ⇒ true
Try in REPL
valid(
  new Date("2020-01-42:52:31.618Z"),
);
// ⇒ false
Try in REPL
valid(new Date("test"));
// ⇒ false
Try in REPL
valid(undefined);
// ⇒ false
Try in REPL

Questions

  • How to check if a Date is valid or not?

TypeScript sourceJavaScript source