How to manipulate and show a Date Time in Javascript?


In javascript, date time information is represented in a a date object that:

  • encapsulates the epoch time in milliseconds
  • is timezone-agnostic


const now = new Date();
console.log("We are the "
   // getMonth'' function is 0 based. Therefore January is at 0 and not 1
   // the ''getYear'' function is deprecated for ''getFullYear'' - due to a ''year 2000 problem'', this function does not return a valid year number.
console.log("We are in toString (local time) the "+now.toString());
console.log("We are in toLocaleString (local time) the "+now.toLocaleString());
console.log("We are in Date String (local time) the "+now.toDateString());
console.log("We are in ISO String (UTC time) the "+now.toISOString());



Current time

now = new Date(); returns the number of milliseconds in epoch scale and not a date object. It's equivalent to new Date().getTime().


console.log( == new Date().getTime());
console.log((new Date(;

From Calendar Numbers

  • Date level
let date = new Date(2018,3,30);
const time = { "year": date.getFullYear(), "month": date.getMonth(), "day": date.getDate(), "hours": date.getHours(), "minutes": date.getMinutes(), "seconds": date.getSeconds() };
  • At the second level
new Date(1973, 07, 20, 8, 30, 0)

From epoch

With an epoch timestamp

let epoch =; 
console.log("The current epoch is "+epoch);
let date = new Date(epoch);
const time = { "year": date.getFullYear(), "month": date.getMonth(), "day": date.getDate(), "hours": date.getHours(), "minutes": date.getMinutes(), "seconds": date.getSeconds() };

From String

let date = new Date('2022-02-18T09:34:32.255Z');
const time = { "year": date.getFullYear(), "month": date.getMonth(), "day": date.getDate(), "hours": date.getHours(), "minutes": date.getMinutes(), "seconds": date.getSeconds() };
let epoch = Date.parse('2022-02-18T09:34:32.255Z');
let date = new Date(epoch );
const time = { "year": date.getFullYear(), "month": date.getMonth(), "day": date.getDate(), "hours": date.getHours(), "minutes": date.getMinutes(), "seconds": date.getSeconds() };


To Iso8601 String

Time in Iso Format:

  • at UTC+00:00 (toISOString does not have any parameters, you can't change the zone)
date = new Date();
  • Now in a Iso String format at UTC
  • at the Locale zone (browser).
function getCurrentLocalDateTimeISO() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0'); // January is 0
  const day = String(now.getDate()).padStart(2, '0');
  const hour = String(now.getHours()).padStart(2, '0');
  const minute = String(now.getMinutes()).padStart(2, '0');
  const second = String(now.getSeconds()).padStart(2, '0');

  const localDateTimeISO = `${year}-${month}-${day}T${hour}:${minute}:${second}`;
  return localDateTimeISO;
  • Now in a Iso String format Local browser time:

To Date String

let date= new Date();

To Only Time Part (Hour, Min, Sec)

let date= new Date();
console.log(date.toLocaleTimeString()); // without zone
console.log(date.toTimeString()); // with zone

To Custom String with string format such as SQL Timestamp String

Custom: There is already a lot of toString functions but none of them give the ability to pass a string format.

The whole Javascript formatting is based on the Intl.DateTimeFormat function that permits to:

  • Format directly with its format function
  • Extract the formatted parts with the formatToParts function

Unfortunately, this function does not support a date format string pattern.

For example, the SQL timestamp string

  • the YYYY-MM-DD date level format with parts extraction
const date = new Date(2018,3,1);
const format = Intl.DateTimeFormat(window.navigator.language, {
  day: "2-digit", 
  year: "numeric", 
  month: "2-digit"
const formatParts = Object.fromEntries(
   format.formatToParts(date).map(e=> [e.type,e.value])
dateString = `${formatParts.year}-${formatParts.month}-${}`;
date = new Date();
dateString = `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2,'0')}-${date.getDate().toString().padStart(2,'0')} ${date.getHours().toString().padStart(2,'0')}:${date.getMinutes().toString().padStart(2,'0')}:${date.getSeconds().toString().padStart(2,'0')}`;

to Locale Relative Time string

If you want to output time in a Locale relative to today, you can use the RelativeTimeFormat

Example with French

const relativeOptions = { numeric: 'auto' }; // Intl.RelativeTimeFormatOptions
const relativeFormatter = new Intl.RelativeTimeFormat("fr", relativeOptions);

Add / Subtstract

var today = new Date();
console.log("Today: "+today);
var tomorrow = new Date(today.getTime()+1000*60*60*24);
console.log("Tomorrow: "+tomorrow);


You can't compare date directly, you need to transform it to the time (a number that is the number of ms since epoch) and compare it.


var today = new Date();
var expirationDate = new Date(today.getTime() - 30);
if (expirationDate.getTime() < today.getTime()) {
  console.log("The date has expired");


See Javascript - TimeZone

High Resolution Time

The following script may record a positive number, negative number, or zero for computed duration: High Resolution API tries to correct this behavior 2)

var mark_start =;
doTask(); // Some task
var duration = - mark_start;



Discover More
Card Puncher Data Processing
Datacadamia - Data all the things

Computer science from a data perspective
Javascript - String

The in javascript. A string in JavaScript is encoded with the ucs-2 16-bit character set. An element of a JavaScript string is therefore a 16-bit code unit. code unitscode pointssurrogate pair Strings...
Javascript - Time

Javascript - Type

Type in Javascript Javascript is not static typed but it exist Javascript wrapper language that implements it. See Only values have types in JavaScript; variables are just simple containers for those...
Puppeteer - How to pass back and forth a date (or a complex type) to the headless browser via the evaluate function

A step by step guide that shows how to serialize and deserialize an object with a date ( ) when using the puppeteer evaluate...

Share this page:
Follow us:
Task Runner