views:

85

answers:

8
+4  Q: 

Date javascript

Hi,

I am trying to understand more about the Date object in javascript. I thought that when you call valueOf(), you get the amount of milliseconds since january 1, 1970. So what I would expect is that the following should return exactly zero;

alert((new Date(1970, 1, 1).valueOf() )/ ( 86400 * 1000));

but it does not, it returns 30.958333333333332. What am I missing?

gr,

Coen

A: 

The method you are searching is .getTime() not .valueOf()

softcr
According to W3Schools, they are the same. A quick self-check - `<html><body><script type="text/javascript">var d=new Date();document.write(d.valueOf() + " milliseconds since 1970/01/01");</script></body></html>` shows that this works (for me, at this exact moment in time.) Do you have a source to contradict this?
Stephen
+6  A: 

new Date(1970, 1, 1) actually is Feb. Months are zero-indexed. Try changing it to new Date(1970, 0, 1).

svanryckeghem
Jeez Javascript. Since when does anyone refer to January as the zeroth month?
jessegavin
@jessegavin: since 1996. http://download.oracle.com/javase/1.4.2/docs/api/java/util/Date.html which is what JavaScript's (actually JScript's) `Date` behavior was derived from.
Crescent Fresh
Numbers should always start with 0, we live in a really messed up world. Just look at the inconsistency of just this constructor. First month is 0, first day is 1, first year is 1970, what the hell?? :p
A: 

Months are zero based in Date objects.

Reinis I.
A: 

January 1st, 1970 is new Date(1970, 0, 1), since months start at 0 = January.

RoToRa
A: 

The first of january 1970 with the Date object is new Date(1970, 0, 1)

Mic
+2  A: 

If you're looking to work with the unix epoch time, you have a few options

  • UTC() Returns the number of milliseconds in a date string since midnight of January 1, 1970, according to universal time
  • setTime() Sets a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970
  • parse() Parses a date string and returns the number of milliseconds since midnight of January 1, 1970
  • getTime() Returns the number of milliseconds since midnight Jan 1, 1970

valueOf() returns a primitive of the value, I'd stay away from it and work with the above options.

source: http://www.w3schools.com/jsref/jsref_obj_date.asp.

edit: also, your asking for Feb 1, 1970

use this, it's dangerous to go alone:

var d=new Date(1970, 0, 1);
document.write(d.getTime());

or

var d= Date.parse("Jan 1, 1970"); //Note, we don't use NEW keyword.
document.write(d);

Remember, epcoh is Wed Dec 31 1969 19:00:00 GMT-0500. If you use .getTime() you'll see UTC time Thu, 01 Jan 1970 00:00:00 GMT +0000.

`valueOf()` returns a representation of the date in a primitive, which is a number in this case. This makes `valueOf()` and `getTime()` equivalent for Date objects.
Andy E
the following does not return either; alert(Date.parse("1970-1-1")/ ( 86400 * 1000))
Coen
@Coen: You should still use `0` as month parameter and it's better not to use the parser to set a date, just use `new Date(1970, 0, 1)`.
Marcel Korpel
@AndyE .valueOf() returns a string, while .getTime() returns a number. This may seem moot, but with the direction of ecma-script, they may be moving to stricter types optionally. Source: ECMAScript 262 spec
@user: *valueOf()* doesn't return a string. Like I said in my original comment, it returns a number primitive. You can try this for yourself - `var test = new Date(); console.log(test.getTime() === test.valueOf())`. Which version of and chapter in the ECMA-262 spec are you referring to?
Andy E
So it looks like there's a bit of confusion with this...15.2.4.4.2.a "Return either O or another value such as the host object originally passed to the constructor. The specific result that is returned is implementation-defined." The refrence I used to say it was a string was http://objjob.phrogz.net/js/object/224 and not http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf. I was wrong. I find it weird that this is implementation-defined however.
@user: *weird* is one word for it. There are a lot of "implementation defined" functions in the spec and it really gets my goat! ;-) Fortunately, *valueOf()* for Date is one of the few the implementors agree on.
Andy E
+2  A: 

Second parameter, month, starts with 0, so you need to do:

alert((new Date(1970, 0, 1).valueOf() )/ ( 86400 * 1000));

but even with this you'll get the offset, in seconds, off GMT.

the value you posted says you are GMT +1 : )

aularon
what is the rationale behind starting months with 0 and days with 1?
Coen
Read the answers to [this](http://stackoverflow.com/questions/393462/defend-zero-based-arrays).
Stephen
We don't talk about zero. http://www.amazon.com/Zero-Biography-Dangerous-Charles-Seife/dp/0140296476
A: 

it was the month that should have been 0 in combination with an hour difference from GMT

alert((new Date(1970, 0, 1, 1, 0, 0, 0).valueOf()));

produces 0

Coen