Last active
January 6, 2021 13:00
-
-
Save donchev7/88229bbb91a6aea3d44afa92002ef444 to your computer and use it in GitHub Desktop.
Working with dates in JavaScript without dependencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//String + Date Format | |
const datePattern = /^(\d{2})-(\d{2})-(\d{4})$/ | |
const [, month, day, year] = datePattern.exec('12-25-1995') | |
new Date(`${month}, ${day} ${year}`) | |
// => "1995-12-24T13:00:00.000Z" | |
//String + Time Format | |
const datePattern = /^(\d{4})-(\d{2})-(\d{2})\s(\d{1,2}):(\d{2})$/ | |
const [, year, month, day, rawHour, min] = datePattern.exec('2010-10-20 4:30') | |
new Date(`${year}-${month}-${day}T${('0' + rawHour).slice(-2)}:${min}:00`) | |
// => "2010-10-19T17:30:00.000Z" | |
//Millisecond / Second / Minute / Hour | |
new Date().getSeconds() | |
// => 49 | |
new Date().getHours() | |
// => 19 | |
new Date(new Date().setSeconds(30)) | |
// => "2018-09-09T09:12:30.695Z" | |
new Date(new Date().setHours(13)) | |
// => "2018-09-09T03:12:49.695Z" | |
//Date of Month | |
new Date().getDate() | |
// => 9 | |
new Date().setDate(4) | |
// => "2018-09-04T09:12:49.695Z" | |
//Day of Week | |
new Date().getDay() | |
// => 0 (Sunday) | |
new Date().setDate(new Date().getDate() - 14) | |
// => "2018-08-26T09:12:49.695Z" | |
//Day of Year | |
Math.floor((new Date() - new Date(new Date().getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24) | |
// => 252 | |
//Week of Year | |
const day = new Date() | |
const MILLISECONDS_IN_WEEK = 604800000 | |
const firstDayOfWeek = 1 // monday as the first day (0 = sunday) | |
const startOfYear = new Date(day.getFullYear(), 0, 1) | |
startOfYear.setDate(startOfYear.getDate() + (firstDayOfWeek - (startOfYear.getDay() % 7))) | |
const dayWeek = Math.round((day - startOfYear) / MILLISECONDS_IN_WEEK) + 1 | |
// => 37 | |
const day = new Date() | |
const week = 24 | |
const MILLISECONDS_IN_WEEK = 604800000 | |
const firstDayOfWeek = 1 // monday as the first day (0 = sunday) | |
const startOfYear = new Date(day.getFullYear(), 0, 1) | |
startOfYear.setDate(startOfYear.getDate() + (firstDayOfWeek - (startOfYear.getDay() % 7))) | |
const dayWeek = Math.round((day - startOfYear) / MILLISECONDS_IN_WEEK) + 1 | |
day.setDate(day.getDate() - (dayWeek - week) * 7) | |
day.toISOString() | |
// => "2018-06-10T09:12:49.794Z | |
//Days in Month | |
new Date(2012, 02, 0).getDate() | |
// => 29 | |
//Maximum / Minimum of the given dates | |
const array = [ | |
new Date(2017, 4, 13), | |
new Date(2018, 2, 12), | |
new Date(2016, 0, 10), | |
new Date(2016, 0, 9), | |
] | |
new Date(Math.max.apply(null, array)).toISOString() | |
// => "2018-03-11T13:00:00.000Z" | |
new Date(Math.min.apply(null, array)).toISOString() | |
// => "2016-01-08T13:00:00.000Z" | |
//Add the specified number of days to the given date. | |
const now = new Date() | |
now.setDate(now.getDate() + 7) | |
// => "Sun Sep 16 2018 09:12:49" | |
//Subtract the specified number of days from the given date. | |
new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 7) | |
// => Sun Sep 09 2018 09:12:49 | |
//Return time from now. | |
new Intl.RelativeTimeFormat().format(-4, 'day') | |
// => "4 days ago" | |
//Get the unit of time between the given dates. | |
new Date(2007, 0, 27) - new Date(2007, 0, 29) | |
// => -172800000 | |
Math.ceil((new Date(2007, 0, 27) - new Date(2007, 0, 29)) / 1000 / 60 / 60 / 24) | |
// => -2 | |
//Check if a date is before another date. | |
new Date(2010, 10, 20) < new Date(2010, 10, 21) | |
// => true | |
//Check if a date is the same as another date. | |
new Date(2010, 9, 20).valueOf() === new Date(2010, 9, 21).valueOf() | |
// => false | |
new Date(2010, 9, 20).valueOf() === new Date(2010, 9, 20).valueOf() | |
// => true | |
new Date(2010, 9, 20).getTime() === new Date(2010, 9, 20).getTime() | |
// => true | |
new Date(2010, 9, 20).valueOf() === new Date(2010, 9, 20).getTime() | |
// => true | |
new Date(2010, 9, 20).toDateString().substring(4, 7) === new Date(2010, 9, 21).toDateString().substring(4, 7) | |
// => true | |
//Is Leap Year | |
new Date(2000, 1, 29).getDate() === 29 | |
// => true | |
//Check if a variable is a native js Date object. | |
new Date() instanceof Date | |
// => true | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment