如何从 JavaScript 日期中删除时间部分?

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

How do I remove time part from JavaScript date?

javascript

提问by Phill Greggan

I have a date '12/12/1955 12:00:00 AM'stored in a hidden column. I want to display the date without the time. How do I do this?

我有一个'12/12/1955 12:00:00 AM'存储在隐藏列中的日期。我想显示没有时间的日期。我该怎么做呢?

采纳答案by Ibrahim Khan

Split it by spaceand take first part like below. Hope this will help you.

将其拆分space并采用如下所示的第一部分。希望这会帮助你。

var d = '12/12/1955 12:00:00 AM';
d = d.split(' ')[0];
console.log(d);

回答by Cerbrus

Parse that string into a Dateobject:

将该字符串解析为Date对象:

var myDate = new Date('12/12/1955 12:00:00 AM');

Then use the usual methodsto get the date's day/ month/ year.

然后使用通常的方法来获取日期的//

回答by Vijay Jagdale

This is probably the easiest way:

这可能是最简单的方法:

new Date(<your-date-object>.toDateString());

Example: To get the Current Date without time component:

示例:要获取没有时间组件的当前日期:

new Date(new Date().toDateString());

gives: Thu Jul 11 2019 00:00:00 GMT-0400 (Eastern Daylight Time)

给出:2019 年 7 月 11 日星期四 00:00:00 GMT-0400(东部夏令时间)