Javascript 从javascript中的日期中删除本地时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30535691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove the local timezone from a date in javascript?
提问by jpganz18
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.
我想知道是否可以使用 javascript 从日期中删除时区?我得到了 json 格式的日期时间,现在我想将它设置回来,但它返回更多我真正想要/需要的信息。
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).
我想只显示到小时,并删除 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.
我知道 javascript 根据当前用户的时区进行处理,这是一个问题,因为信息可以保存在不同的国家/地区。
I am trying to avoid to create the format using something like:
我试图避免使用以下内容创建格式:
d.date() + "/" + d.month()...etc
Is there a better solution for this?
有没有更好的解决方案?
采纳答案by gautsch
I believe d.toDateString()will output the format you're looking for.
我相信d.toDateString()会输出您正在寻找的格式。
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"
甚至d.toLocaleString()“2015 年 5 月 28 日下午 6:10:21”
There are lots of methods available to Date()
Date()有很多可用的方法
回答by Samurai
回答by victorkurauchi
with momentjs
使用momentjs
var d = new Date();
d.setTime(1432851021000);
var newdate = moment(d);
alert(newdate.format("MMM DD/MM/YYYY HH:mm:ss "));
回答by Chris
var date = new Date(myUnixTimestamp*1000);
var prettyDate = date.toLocaleString();
回答by Apoorva Shah
I have created simple js function to remove timezone part from datetime string. It which will give output like
我创建了简单的 js 函数来从日期时间字符串中删除时区部分。它会给出类似的输出
- 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
- 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'));
回答by Anshul Bansal
I feel there is a better yet simpler solution!
我觉得有一个更好但更简单的解决方案!
Extract the Day, Month & Year from your "Date Variable".
- var day = d.getDate(); //to get the day
- var month = d.getMonth(); // to get the month
- var year = d.getYear(); // to get the year
Now just assign a new variable like
- var newDate = day+"/"+(month+1)+"/"+year; //Note, we are doing (month+1) because in Javascript the month varibale runs from {0-11} (0: Jan , 1:Feb,2: Mar, 4: Apr.... 11:Dec)
从“日期变量”中提取日、月和年。
- var day = d.getDate(); //获取日期
- var 月 = d.getMonth(); // 获取月份
- var 年 = d.getYear(); // 获取年份
现在只需分配一个新变量,如
- var newDate = day+"/"+(month+1)+"/"+year; //注意,我们正在做 (month+1) 因为在 Javascript 中,月份变量从 {0-11} (0: Jan , 1:Feb,2: Mar, 4: Apr .... 11:Dec) 运行
And there you have it, your customized date without timezone is ready!
有了它,您的不带时区的自定义日期就准备好了!
回答by Sujith
var x = d.toDateString().length;
var result = d.toDateString() + d.toString().substring(x,x+9);
回答by Tarek
You can use 2 options:
1. use split function: d.split('GMT'). this gives you an array.
2. get the usual js data functions like getHour and getFullYear and so on to format the result as you prefer.
您可以使用 2 个选项:
1. 使用拆分函数:d.split('GMT')。这给你一个数组。
2. 获取常用的 js 数据函数,如 getHour 和 getFullYear 等,以根据您的喜好格式化结果。

