Javascript 从 GMT 时间格式中删除时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27869606/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:49:21  来源:igfitidea点击:

Remove time from GMT time format

javascriptdateutcgmt

提问by Grady D

I am getting a date that comes in GMT format, Fri, 18 Oct 2013 11:38:23 GMT. The problem is that the time is messing up the timeline that I am using.

我得到一个 GMT 格式的日期,2013 年 10 月 18 日星期五 11:38:23 GMT。问题是时间弄乱了我正在使用的时间线。

How can I strip out everything except for the actual date?

除了实际日期之外,我怎样才能去掉所有内容?

回答by Gio Asencio

If you want to keep using Date and not String you could do this:

如果你想继续使用 Date 而不是 String 你可以这样做:

var d=new Date(); //your date object
console.log(new Date(d.setHours(0,0,0,0)));

-PS, you don't need a new Date object, it's just an example in case you want to log it to the console.

-PS,您不需要新的 Date 对象,这只是一个示例,以防您想将其记录到控制台。

http://www.w3schools.com/jsref/jsref_sethours.asp

http://www.w3schools.com/jsref/jsref_sethours.asp

回答by Inanda Menezes

Like this:

像这样:

var dateString = 'Mon Jan 12 00:00:00 GMT 2015';
dateString = new Date(dateString).toUTCString();
dateString = dateString.split(' ').slice(0, 4).join(' ');
console.log(dateString);

回答by Ellone

I'm using this workaround :

我正在使用此解决方法:

// d being your current date with wrong times
new Date(d.getFullYear(), d.getMonth(), d.getDate())

回答by Sakshi Agarwal

You can first convert the date to String:

您可以先将日期转换为字符串:

String dateString = String.valueOf(date);

String dateString = String.valueOf(date);

Then apply substringto the String:

然后将子字符串应用于字符串:

dateString.substring(4, 11) + dateString.substring(30);

dateString.substring(4, 11) + dateString.substring(30);

You need to take care as converting date to String will actually change the date format as well.

您需要小心,因为将日期转换为字符串实际上也会更改日期格式。

回答by Mike

You could use Moment.js, a library that provides many helper functions to validate, manipulate, display and format dates and times in JavaScript.

您可以使用Moment.js,该库提供了许多帮助函数来验证、操作、显示和格式化 JavaScript 中的日期和时间。

Using Moment.js lib:

使用 Moment.js 库:

var dateString = new Date('Mon Jan 12 00:00:00 GMT 2015');
moment(dateString).format('YYYY-MM-DD HH:mm');

Or simplified:

或简化:

moment('Mon Jan 12 00:00:00 GMT 2015').format('YYYY-MM-DD HH:mm')

回答by Nixon Kosgei

Well,

好,

Here is my Solution

这是我的解决方案

let dateString = 'Mon May 25 01:07:00 GMT 2020';
let dateObj = new Date(dateString);

console.log(dateObj.toDateString());
// outputs Mon May 25 2020

See its documentation on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString

请参阅 MDN 上的文档https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString

回答by Tomer

Just cut it with substring:

只需用以下方法剪掉它substring

 var str = 'Fri, 18 Oct 2013 11:38:23 GMT';
 str = str.substring(0,tomorrow.toLocaleString().indexOf(':')-3);

回答by Xotic750

In this case you can just manipulate your string without the use of a Dateobject.

在这种情况下,您可以在不使用Date对象的情况下操作您的字符串。

var dateTime = 'Fri, 18 Oct 2013 11:38:23 GMT',
    date = dateTime.split(' ', 4).join(' ');
    
document.body.appendChild(document.createTextNode(date));