X
Home
Topics
Control Panels
Hosting
Linux & Serverless
WordPress
PHP
Javascript
React & React Native
CSS (& HTML)
Node.js
SQL & No SQL
Miscellaneous
About
Contact
Resources
Archive

the Basics of Dates and Times in Javascript

just print current UTC time
let n = new Date()
console.log(n.toISOString())
// this is UTC time
// => 2021-10-18T14:54:07.165Z

print LOCAL date only
let n = new Date()
let nf = Intl.DateTimeFormat(‘en-US’).format(n);
console.log(nf);
// => 10/18/2021

highly customized
let d5 = new Date(‘2021-10-18T15:02:51.235Z’)
let options = {
weekday: ‘short’,
month: ‘short’,
year: ‘numeric’,
day: ‘numeric’,
hour: ‘numeric’,
minute: ‘2-digit’,
timeZone: ‘America/New_York’
};
let dCustom = Intl.DateTimeFormat(‘en-US’, options).format(d5);
console.log(dCustom)
// => Mon, Oct 18, 2021, 11:02 AM

completely customized

let h = d4.getUTCHours();
// below is your local time
let hlocal = d4.getHours();
let m = d4.getUTCMinutes();

console.log(m);
console.log(hlocal);
console.log(h);
// then construct your custom string

get date and time (local) for exactly one week out from today

let options = {
weekday: ‘short’,
month: ‘short’,
year: ‘numeric’,
day: ‘numeric’,
hour: ‘numeric’,
minute: ‘2-digit’,
timeZone: ‘America/New_York’
};

let today = new Date();

let weekFromToday = today.setDate(today.getDate() + 7);
let formated = Intl.DateTimeFormat(‘en-US’, options).format(weekFromToday)

console.log(formated);

get the time thirty minutes out from right now, in local time

let now = new Date();

let thirtyMinsLater = now.setMinutes(now.getMinutes() + 30);
let timesUp = Intl.DateTimeFormat(‘en-US’, options).format(thirtyMinsLater)

console.log(timesUp);