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
Remove time from GMT time format
提问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 对象,这只是一个示例,以防您想将其记录到控制台。
回答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);

