Javascript Date() object with NULL Time

26,788

Solution 1

Not really. A Javascript Date object 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.

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 are midnight. 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 time component are arbitrary and won't be used, but they still need to be there if you want to create a Date object.

Solution 2

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

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 Spec See page 165

From ECMA standard:

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.

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.

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

Solution 3

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 toString that checks the time_is_meaningful property and acts accordingly.

Solution 4

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.

Share:
26,788
Anson Kao
Author by

Anson Kao

Hi! My name is Anson, and I'm based out of Toronto. I'm an entrepreneur with a decade-long background in technology, startups, and more recently: video. I love bringing people together to create the future.

Updated on July 09, 2022

Comments

  • Anson Kao
    Anson Kao almost 2 years

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

    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 explicitly inputed both a Date and a Time