remove time zone from date in javascript

How to Remove Local Timezone from Date in JavaScript

JavaScript date values contain local time zone information which may not be required in many cases. In fact, it can even be misguiding if your users are located in different time zones. Due to the inclusion of local time zone information the client browser may end up showing different date values for different users, depending on their time zones. So it is advisable to remove local time zone from date in JavaScript. In this article, we will learn how to remove local time zone from date in JavaScript.


How to Remove Local Timezone from Date in JavaScript

Typically, here is the information you get when you create a JS date.

var date = new Date();

Here is what this variable will look like.

Tue Sep 06 2021 00:00:00 GMT+0200

If you convert it into ISOString, you get the following information.

var isoDate = date.toISOString()
'2021-09-05T22:00:00.000Z'

As you can see above, the time changes when you convert the date time into ISO String.

One of the simplest ways to remove time zone information is to use moments library in JS.

var clearUTCDate = moment(date).utcOffset(0, true).format() 
'2021-09-06T00:00:00Z'

Alternatively, you can also use toDateString() function.

console.log(date.toDateString());
Tue Sep 06 2021

Or you can also use toLocaleString() function as shown below.

console.log(date.toLocaleString());
9/06/2021, 00:00:00

In this short article, we have learnt several simple ways to remove time zone information from JavaScript date values. You can use any of them as per your requirement.

Also read:

How to Format Date to MMDDYYYY in JavaScript
How to Copy to Clipboard in JavaScript
How to Get Query String Values in JavaScript
How to Check if Object is Empty in JavaScript
How to Return Response from Asynchronous Call in JS

Leave a Reply

Your email address will not be published.