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?
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 .
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 .
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.
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.
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()
Since the part you want will always has fixed length you can use:
d.toString().slice(0, 24)
jsfiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With