Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove the local timezone from a date in javascript?

I was wondering if I can remove the timezone from the date using javascript? I get the datetime on a json format, now I want to set it back but it returns more info of what I actually want/need.

What I have now is:

var d = new Date(); d.setTime(1432851021000); document.write(d); 

and the output is something like this:

Thu May 28 2015 16:10:21 GMT-0600 (CST) 

Id like to show only until the hour, and remove GMT-0600 (CST).

I know javascript takes that depending on the current user's timezone, which is a problem, because the information could be saved on different countries.

I am trying to avoid to create the format using something like:

d.date() + "/" + d.month()...etc 

Is there a better solution for this?

like image 524
jpganz18 Avatar asked May 29 '15 17:05

jpganz18


People also ask

How do I remove time part from JavaScript date?

To remove the time from a date, use the getFullYear() , getMonth() and getDate() methods to get the year, month and date of the given date and pass the results to the Date() constructor. When values for the time components are not provided, they default to 0 .

How can I date without a time zone?

To create a Date without the timezone, we called the toISOString method on the Date object and removed the character Z from the ISO string. The Date object shows the exact same time as the one stored in the dateStr variable - 09:35:31 .

How do I remove Tzinfo from DateTime?

To remove timestamp, tzinfo has to be set None when calling replace() function. First, create a DateTime object with current time using datetime. now(). The DateTime object was then modified to contain the timezone information as well using the timezone.

Does JavaScript date have timezone?

JavaScript's internal representation uses the “universal” UTC time but by the time the date/time is displayed, it has probably been localized per the timezone settings on the user's computer. And, indeed, that's the way JavaScript is set up to work.


2 Answers

I believe d.toDateString() will output the format you're 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 lots of methods available to Date()

like image 141
gautsch Avatar answered Sep 22 '22 19:09

gautsch


Since the part you want will always has fixed length you can use:

d.toString().slice(0, 24) 

jsfiddle

like image 28
Samurai Avatar answered Sep 18 '22 19:09

Samurai