Last active
December 20, 2019 19:51
-
-
Save okovalov/9dc92cb325de00545abd682b68b0b34e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Checks if dates are on the same day | |
* | |
* @param {Object} first First date to comapre | |
* @param {Object} second Second date to compare | |
*/ | |
const datesAreOnSameDay = (first, second) => | |
first.getFullYear() === second.getFullYear() && | |
first.getMonth() === second.getMonth() && | |
first.getDate() === second.getDate() | |
datesAreOnSameDay(new Date(), new Date()) // true | |
// how can we get the month name? | |
const today = new Date() | |
console.log(today.toLocaleString('default', { month: 'long' })) // October | |
console.log(today.toLocaleString('default', { month: 'short' })) // Oct | |
// How do you get yesterdays' date | |
const yesterday = new Date(today) | |
yesterday.setDate(yesterday.getDate() - 1) | |
console.log(today.toDateString()) // Tue Oct 29 2019 | |
console.log(yesterday.toDateString()) // Mon Oct 28 2019 | |
// How do you get tomorrow’s date | |
const tomorrow = new Date(today) | |
tomorrow.setDate(tomorrow.getDate() + 1) | |
// If you also want to reset the time to “tomorrow at 00:00:00” | |
// tomorrow.setHours(0,0,0,0) | |
console.log(tomorrow.toDateString()) | |
/** | |
* Returns an object containing difference between a given date string and now | |
* | |
* @param {string} dateString A string representation of the date (a result of dateObj.toString() call ) | |
* @param {string} type Type of the difference to returs (minute, hour etc) | |
*/ | |
const dateDiffWith = (dateString, type = 'minute') => { | |
const _today = new Date() | |
const pastDate = new Date(dateString) | |
const diffMs = _today - pastDate // milliseconds between pastDate and now | |
const diffDays = Math.floor(diffMs / 86400000) // days | |
const diffHrs = Math.floor((diffMs % 86400000) / 3600000) // hours | |
const diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000) // minutes | |
const result = { | |
ms: diffMs, | |
day: diffDays, | |
hour: diffHrs, | |
minute: diffMins, | |
} | |
return result[type] | |
} | |
const dateDiffResult = dateDiffWith(tomorrow.toString()) | |
console.log('dateDiffResult result', dateDiffResult) // dateDiffResult result -60 | |
/* | |
* Count the number of nights that a person had to pay to rent a house and sleep in it, | |
* depending on the checkin date, and the checkout date. | |
* | |
* from - https://flaviocopes.com/how-to-count-days-between-dates-javascript/ | |
*/ | |
const numberOfNightsBetweenDates = (startDate, endDate) => { | |
const start = new Date(startDate) //clone | |
const end = new Date(endDate) //clone | |
let dayCount = 0 | |
/* | |
* If instead you want to get the number of days between 2 dates (say, today to tomorrow is 2 days), | |
* just change while (end > start) to while (end >= start). | |
*/ | |
while (end > start) { | |
dayCount++ | |
start.setDate(start.getDate() + 1) | |
} | |
return dayCount | |
} | |
// How to format a date in JavaScript | |
// from https://flaviocopes.com/how-to-format-date-javascript/ | |
const date = new Date('July 22, 2018 07:22:13') | |
date.toString() // "Sun Jul 22 2018 07:22:13 GMT+0200 (Central European Summer Time)" | |
date.toTimeString() //"07:22:13 GMT+0200 (Central European Summer Time)" | |
date.toUTCString() //"Sun, 22 Jul 2018 05:22:13 GMT" | |
date.toDateString() //"Sun Jul 22 2018" | |
date.toISOString() //"2018-07-22T05:22:13.000Z" (ISO 8601 format) | |
date.toLocaleString() //"22/07/2018, 07:22:13" | |
date.toLocaleTimeString() //"07:22:13" | |
date.getDate() //22 | |
date.getDay() //0 (0 means sunday, 1 means monday..) | |
date.getFullYear() //2018 | |
date.getMonth() //6 (starts from 0) | |
date.getHours() //7 | |
date.getMinutes() //22 | |
date.getSeconds() //13 | |
date.getMilliseconds() //0 (not specified) | |
date.getTime() //1532236933000 | |
/* | |
(will vary depending on where you are and when you check - this is CET during the summer). | |
Returns the timezone difference expressed in minutes | |
*/ | |
date.getTimezoneOffset() //-120 | |
/* | |
Those all depend on the current timezone of the computer. | |
There are equivalent UTC versions of these methods, | |
that return the UTC value rather than the values adapted to your current timezone: | |
*/ | |
date.getUTCDate() //22 | |
date.getUTCDay() //0 (0 means sunday, 1 means monday..) | |
date.getUTCFullYear() //2018 | |
date.getUTCMonth() //6 (starts from 0) | |
date.getUTCHours() //5 (not 7 like above) | |
date.getUTCMinutes() //22 | |
date.getUTCSeconds() //13 | |
date.getUTCMilliseconds() //0 (not specified) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment