Remove the local Time Zone from a Date using JavaScript

avatar

Borislav Hadzhiev

Tue Jan 18 20223 min read

Remove the local Time Zone from a Date using JavaScript #

There are multiple ways to remove the local time zone from a date:

  1. Use the toUTCString() method to convert the date to a string using UTC.
  2. Use the toLocaleDateString() method to format the date according to a specific locale.
  3. Format the date and time consistently, e.g. as YYYY-MM-DD hh:mm:ss.
index.js
const date = new Date(); // ✅ Get UTC string without local time zone // 👇️ Tue, 18 Jan 2022 14:30:00 GMT console.log(date.toUTCString()); // ✅ Get local representation of Date and time (en-US locale) // 👇️ 1/18/2022, 4:30:00 PM console.log(date.toLocaleString('en-US')); // ✅ Get local representation of Date and time (en-GB locale) // 👇️ 18/01/2022, 16:30:00 console.log(date.toLocaleString('en-GB')); // ✅ Format date and time as YYYY-MM-DD hh:mm:ss // (or any other variation by adjusting the code in the function) function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return ( [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-') + ' ' + [ padTo2Digits(date.getHours()), padTo2Digits(date.getMinutes()), padTo2Digits(date.getSeconds()), ].join(':') ); } // 👇️ 2022-01-18 16:30:00 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date())); // 👇️️ 2025-05-04 05:24:07 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date('May 04, 2025 05:24:07')));

The toUTCString method returns a string representation of the date using the UTC time zone.

index.js
const date = new Date(); // ✅ Get UTC string without local time zone // 👇️ Tue, 18 Jan 2022 14:30:00 GMT console.log(date.toUTCString());

If you need to format the date and time according to a specific locale, you can use the toLocaleString method.

Here are examples for the en-US and en-GB locales.

index.js
// ✅ Get local representation of Date and time (en-US locale) // 👇️ 01/18/2022, 4:30:00 PM console.log( date.toLocaleString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', }), ); // ✅ Get local representation of Date and time (en-GB locale) // 👇️ 18/01/2022, 16:30:00 console.log( date.toLocaleString('en-GB', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', }), );

The two parameters we passed to the toLocaleString method are:

  1. locales - a string with a BCP 47 language tag or an array of such strings. You can use any of the available locales, e.g. es-MX for Mexico or en-CA for Canada. If you need more information about this parameter, check out the MDN docs.
  2. options object where we set how the date and time components should be formatted. Read more about the options object in the MDN docs.

We used the options object to set most of the date and time components formatted as 2-digits for consistency.

If you need more control as to how you format the date and time, you can write a function that takes care of formatting them.

index.js
// ✅ Format date and time as YYYY-MM-DD hh:mm:ss // (or any other variation by adjusting the code in the function) function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return ( [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-') + ' ' + [ padTo2Digits(date.getHours()), padTo2Digits(date.getMinutes()), padTo2Digits(date.getSeconds()), ].join(':') ); } // 👇️ 2022-01-18 16:30:00 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date())); // 👇️️ 2025-05-04 05:24:07 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date('May 04, 2025 05:24:07')));

The function makes use of the following 6 Date related methods.

  • Date.getFullYear method - returns a four-digit number representing the year that corresponds to a date.

  • Date.getMonth - returns an integer between 0 (January) and 11 (December) and represents the month for a given date. Yes, unfortunately the getMonth method is off by 1.

  • Date.getDate - returns an integer between 1 and 31 representing the day of the month for a specific date.

  • Date.getHours - returns the hour for the specified date.

  • Date.getMinutes - returns the minutes for a date.

  • Date.getSeconds - returns the seconds of a specific date.

The getMonth method returns a zero-based month index from 0 to 11, meaning January is 0 and December is 11.

Because the getMonth method is zero-based we added 1 to its return value.

We placed the year, month and day in an array, so we can join them with a hyphen separator.

index.js
console.log(['2022', '01', '18'].join('-')); // 👉️ '2022-01-18' console.log(['2028', '04', '24'].join('-')); // 👉️ '2026-04-24'

This gets us the date formatted as yyyy-mm-dd.

The next step is to place the return values from the time-related methods in an array and join them with a colon.

index.js
console.log(['05', '24', '36'].join(':')); // 👉️ '05:24:36' console.log(['08', '13', '56'].join(':')); // 👉️ '08:13:56'

We used the addition (+) operator to add a space in the middle of the strings to get the date formatted as yyyy-mm-dd hh:mm:ss.

You can reorder the date components, change the separator to a forward slash /, instead of a hyphen, or make any changes that suite your use case.
Use the search field on my Home Page to filter through my more than 1,000 articles.