从javascript中的日期中删除本地时区? [英] remove the local timezone from a date in javascript?
问题描述
我想知道是否可以使用javascript从日期中删除时区?我将日期时间设为json格式,现在我想将其设置回去,但是它返回我实际想要/需要的更多信息。
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.
我现在所拥有的是:
var d = new Date();
d.setTime(1432851021000);
document.write(d);
,输出结果如下:
Thu May 28 2015 16:10:21 GMT-0600 (CST)
Id只显示到一个小时,然后删除GMT-0600(CST)。
Id like to show only until the hour, and remove GMT-0600 (CST).
我知道javascript会根据当前用户的时区来执行此操作,这是一个问题,因为信息可以保存在不同的国家。
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?
推荐答案
我相信 d.toDateString()
将输出所需的格式。
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"
甚至 d.toLocaleString()
5/28/2015,6:10:21 PM
Or even d.toLocaleString()
"5/28/2015, 6:10:21 PM"
有 Date()有很多方法
这篇关于从javascript中的日期中删除本地时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!