Remove local timezone from date in javascript?
I was wondering if I can remove the timezone from a date using javascript? I am getting datetime in json format, now I want to set it back, but it returns more information about what I really want / need.
Now I have:
var d = new Date();
d.setTime(1432851021000);
document.write(d);
and the output looks something like this:
Thu May 28 2015 16:10:21 GMT-0600 (CST)
Id like to only show up to the hour and remove GMT-0600 (CST).
I know javascript accepts this based on the user's current timezone, which is a problem because the information can be stored in different countries.
I am trying to avoid creating a format using something like:
d.date() + "/" + d.month()...etc
Is there a better solution for this?
source to share
I believe it d.toDateString()
will output the format you are looking for.
var d = new Date();
d.setTime(1432851021000);
d.toDateString(); // outputs to "Thu May 28 2015"
d.toGMTString(); //outputs to "Thu, 28 May 2015 22:10:21 GMT"
Or even d.toLocaleString()
"5/28/2015, 6:10:21 PM"
There are many methods for Date ()
source to share
I created a simple js function to remove part of the timezone from a datetime string. This which will give output like
- 2019-07-27T19: 00: 00.000-0700 => 2019-07-27T19: 00: 00.000
- 2019-09-17T19: 00: 00.000 + 0100 => 2019-09-17T19: 00: 00.000
function removeTimeZonePart(dateString) {
let finalDate = '';
if (dateString.split('+').length > 1) {
let b = dateString.split('+');
finalDate = b[0];
} else {
let b = dateString.split('-');
if (b.length > 1) {
b.pop();
finalDate = b.join('-');
}
}
return finalDate;
}
alert(removeTimeZonePart('2019-07-27T19:00:00.000-0700'));
source to share