James Sherry PRO
Web Development Tutor and Co-Founder of { The Jump } Digital School
const now = new Date();
// e.g. Mon Oct 14 2019 14:48:26 GMT+0100 (British Summer Time)
console.log(now);
// The timestamp: 1571060906179
console.log(now.getTime());
// Date(year, monthIndex, date, hour, min, seconds, milliseconds)
const date = new Date(2016, 6, 27, 13, 30, 0, 0);
// OR with timestamp (e.g. from date.getTime() of another?)
const date2 = new Date(1571065751456);
// OR With UTC date string (z or 'zulu' is the timezone)
const date = new Date("2016-07-27T13:30:00Z");
const date3 = new Date("Wed, 27 July 2016 13:30:00");
To create a date we pass the Date constructor:
year (required)
Integer value representing the year. Values from 0 to 99 map to the years 1900 to 1999; all other values are the actual year.
monthIndex (required)
Integer value representing the month, beginning with 0 for January to 11 for December.
day (optional)
Integer value representing the day of the month. If not specified, the default value of 1 is used.
hours (optional)
Integer value representing the hour of the day. The default is 0 (midnight).
minutes (optional)
Integer value representing the minute segment of a time. The default is 0 minutes past the hour.
seconds (optional)
Integer value representing the second segment of a time. The default is zero seconds past the minute.
milliseconds (optional)
Integer value representing the millisecond segment of a time. The default is 0 milliseconds past the second.
Shown earlier...
credits toptal.com
Dates provided with different strings may be difficult to compare!!
function isDate(d) {
return d instanceof Date ;
}
function isValidDate(d) {
// Note: we use the old window.isNaN() here
return d instanceof Date && !isNaN(d);
}
const date1 = new Date(2016, 6, 25);
const date2 = new Date(2016, 6, 26);
// Are 2 dates the same
date1.getTime() === date2.getTime()
// For same date with different time you have to do this manually
// Earlier or later (greater === later)
date1 > date2
// adding/subtracting
const originalDate = new Date(2016, 6, 20, 15);
console.log('oldDate', originalDate.toLocaleString());
const nextDate = new Date(originalDate.getTime()); // How to clone a date
const newDateNumber = originalDate.getDate() + 20; // + 20 days
nextDate.setDate(newDateNumber);
const newDateSring = originalDate.toLocaleString();
console.log('newDate', newDateSring);
// Diffing between two times
const date1 = new Date();// some time in the past
const date2 = new Date(); // now
const milliSecondsDiff = Math.abs(date1.getTime() - date2.getTime());
// Number of milliseconds per day =
// 24 hrs/day * 60 minutes/hour * 60 seconds/minute * 1000 msecs/second
const daysDiff = Math.floor(milliSecondsDiff/(1000 * 60 * 60 * 24));
console.log(daysDiff);
const dateTime = new Date();
// Local Time
const date = dateTime.getDate(); // 1 - 31
const day = dateTime.getDay(); // 0 - 6 (For name, use either an array or Intl.format)
const month = dateTime.getMonth(); // 0 - 11
// USE INSTEAD OF getYear
const year = dateTime.getFullYear(); // Gives 4 digits, if created that way
const hour = dateTime.getHours(); // 0 - 23
const minute = dateTime.getMinutes(); // 0 - 59
const seconds = dateTime.getSeconds(); // 0 - 59
const milliseconds = dateTime.getMilliseconds(); // 0 - 999
// Offset from UTC in minutes (so +1 or BST is -60)
const UTC_Offset = dateTime.getTimezoneOffset();
const UTC_Month = dateTime.getUTCMonth(); // Gets the UTC value for the month
export function getDatetime(timestamp) {
const d = new Date(timestamp);
const dateTimeString = `${d.getFullYear()}-${d.getMonth()+1}-${d.getDate()} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}.${d.getMilliseconds()}`;
return dateTimeString; // 2011-11-18 14:54:39.929
}
const userLocale =
navigator.languages && navigator.languages.length
? navigator.languages[0]
: navigator.language;
const today = new Date().toLocaleDateString(
'en-GB', // locale
{ // options
day : 'numeric',
month : 'short',
year : 'numeric'
})
const date = new Date(Date.UTC(2022, 11, 28, 3, 23, 16, 738));
const format = new Intl.DateTimeFormat('en-GB', { dateStyle: 'full', timeStyle: 'long', timeZone: 'Australia/Sydney' });
console.log(format(date));
// With fallbacks
console.log(new Intl.DateTimeFormat(['en-US', 'en-GB'], { dateStyle: 'full', timeStyle: 'long', timeZone: 'Australia/Sydney' }).format(date))
(Timezone Addon Package: date-fns-tz)
By James Sherry
A guide to dates in JavaScript, including date-fns (and Moment.js for reference)