The snippet library.
Small, practical examples. Search by task or narrow things down below.
Add calendar days in UTC
JavaScript: Use UTC date setters to cross month boundaries without local daylight-saving effects.
JavaScript✓ Tested
Construct a date from Unix seconds
JavaScript: Multiply seconds by 1000 before creating a Date.
JavaScript✓ Tested
Convert epoch milliseconds to whole seconds
JavaScript: JavaScript Date stores milliseconds while many APIs use Unix seconds.
JavaScript✓ Tested
Count days between UTC-midnight dates
JavaScript: A day is exactly 86400000 milliseconds for these explicitly UTC-midnight inputs.
JavaScript✓ Tested
Detect an invalid Date object
JavaScript: An invalid Date has a NaN time value.
JavaScript✓ Tested
Find the final day of a month
JavaScript: Day zero of the following UTC month selects the previous month’s final day.
JavaScript✓ Tested
Format whole seconds as hours minutes and seconds
JavaScript: Use integer divisions and remainders for a nonnegative duration.
JavaScript✓ Tested
Read an ISO-style weekday number
JavaScript: Convert Sunday from zero to seven when using Monday-through-Sunday numbering.
JavaScript✓ Tested
Read date components in UTC
JavaScript: UTC accessors avoid differences caused by the machine’s local timezone.
JavaScript✓ Tested
Sort timestamp strings by their parsed instants
JavaScript: Compare epoch values explicitly when date strings may contain different offsets.
JavaScript✓ Tested