带有 NULL 时间的 Javascript Date() 对象

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

Javascript Date() object with NULL Time

javascriptdatetimetypes

提问by Anson Kao

Is there a way to indicate only the date portion of a Date() object, without indicating the time?

有没有办法只指示 Date() 对象的日期部分,而不指示时间?

e.g.

例如

var d = Date();
d.setFullYear(2015, 0, 13);
d.toString();

"Tue Jan 13 2015 00:00:00 GMT-0500 (EST)"  // Wrong - I didn't set a time!
"Tue Jan 13 2015     NULL GMT-0500 (EST)"  // Expected Result

I want to be able to tell the difference between a user who only inputed the Date portion, vs one who explicitlyinputed both a Date and a Time

我希望能够区分仅输入日期部分的用户与明确输入日期和时间的用户之间的区别

回答by Andrew Magee

Not really. A Javascript Dateobject always has a time. You can leave it at midnight and ignore it if you want, but it'll still be there. It's up to you how you interpret it.

并不真地。JavascriptDate对象总是有时间的。如果您愿意,您可以在午夜离开它并忽略它,但它仍然会在那里。这取决于你如何解释它。

If you want to be able to represent a null time, you could interpret midnight to mean that, though then you would have no way to represent times that actually aremidnight. If you want to be able to have a null time and still represent every possible time you would need to have two variables.

如果您希望能够表示空时间,您可以将午夜解释为这个意思,但是您将无法表示实际午夜的时间。如果您希望能够有一个空时间并且仍然代表每个可能的时间,您将需要有两个变量。

You could have:

你可以有:

// Date with null time
var date = new Date(2015, 0, 13); // time component ignored
var time = null;

// Date with non-null time
var date = new Date(2015, 0, 13); // time component ignored
var time = new Date(1970, 0, 1, 9, 30); // date component ignored

Note in the second example the year, month and day in the timecomponent are arbitrary and won't be used, but they still need to be there if you want to create a Dateobject.

请注意,在第二个示例中,time组件中的年、月和日是任意的,不会被使用,但如果您想创建Date对象,它们仍然需要存在。

回答by kmcnamee

JavaScript Date objects are internally defined using number of milliseconds since January 1, 1970 UTC. Therefore you are stuck with the time part.

JavaScript Date 对象是使用自 UTC 1970 年 1 月 1 日以来的毫秒数在内部定义的。因此,您被困在时间部分。

Try this code

试试这个代码

var date = new Date(2015, 0, 13);
console.log(date.valueOf());

You'll get this output

你会得到这个输出

1421125200000

Here is the standard definition...

这是标准定义...

ECMAScript Language SpecSee page 165

ECMAScript 语言规范参见第 165 页

From ECMA standard:

来自 ECMA 标准:

A Date object contains a Number indicating a particular instant in time to within a millisecond. Such a Number is called a time value. A time value may also be NaN, indicating that the Date object does not represent a specific instant of time.

一个 Date 对象包含一个 Number ,指示一个特定的时间到一毫秒内。这样的数字称为时间值。时间值也可能是 NaN,表示 Date 对象不代表特定的时刻。

Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC. In time values leap seconds are ignored. It is assumed that there are exactly 86,400,000 milliseconds per day. ECMAScript Number values can represent all integers from –9,007,199,254,740,992 to 9,007,199,254,740,992; this range suffices to measure times to millisecond precision for any instant that is within approximately 285,616 years, either forward or backward, from 01 January, 1970 UTC.

时间在 ECMAScript 中以 UTC 时间 1970 年 1 月 1 日以来的毫秒为单位进行测量。在时间值中,闰秒被忽略。假设每天正好有 86,400,000 毫秒。ECMAScript Number 值可以表示从 –9,007,199,254,740,992 到 9,007,199,254,740,992 的所有整数;此范围足以测量从 UTC 时间 1970 年 1 月 1 日起向前或向后大约 285,616 年内的任何时刻的毫秒精度。

The actual range of times supported by ECMAScript Date objects is slightly smaller: exactly –100,000,000 days to 100,000,000 days measured relative to midnight at the beginning of 01 January, 1970 UTC. This gives a range of 8,640,000,000,000,000 milliseconds to either side of 01 January, 1970 UTC. The exact moment of midnight at the beginning of 01 January, 1970 UTC is represented by the value +0

ECMAScript Date 对象支持的实际时间范围略小:相对于 UTC 时间 1970 年 1 月 1 日开始的午夜,正好是 –100,000,000 天到 100,000,000 天。这为 1970 年 1 月 1 日 UTC 的任一侧提供了 8,640,000,000,000,000 毫秒的范围。UTC 1970 年 1 月 1 日开始的午夜的确切时刻由值 +0 表示

回答by Edgar Bonet

Dates are objects. As such, you can add properties to them as needed:

日期是对象。因此,您可以根据需要向它们添加属性:

var date_only = new Date("2015-03-04");
date_only.time_is_meaningful = false;

var date_with_time = new Date("2015-03-04T10:47");
date_with_time.time_is_meaningful = true;

This is simpler and cleaner than your millisecond hack, and more convenient than having two separate variables. You could then, if you wish, subclass Date with a custom toStringthat checks the time_is_meaningfulproperty and acts accordingly.

这比毫秒黑客更简单、更清晰,也比拥有两个单独的变量更方便。然后,如果您愿意,您可以使用自定义toString检查 time_is_meaningful属性并相应地操作Date 子类。

回答by Greg

You cannot remove the time information from a Date. If you want to have both informations independently, use a Date for the date and ignore the time (e.g. ensure it's always exactly midnight for instance), and use another field to hold the time (it could be a Date where you ignore the date but not the time, or it could be one input text with formatted time, or several input texts with hour, minute, etc). The UI representation is up to you.

您不能从日期中删除时间信息。如果您想独立拥有这两个信息,请使用日期作为日期并忽略时间(例如确保它始终恰好是午夜),并使用另一个字段来保存时间(它可能是一个日期,您忽略日期但不是时间,或者它可以是一个带有格式化时间的输入文本,或者几个带有小时、分钟等的输入文本)。UI 表示由您决定。