Arun
Arun

Reputation: 23

Javascript gettime()

I am trying to use gettime to sort my date string. But it is returning some vague values like.

  1. 1428303000000 16/06/2014 16:50
  2. 1389074040000 01/07/2014 16:54

The first date is smaller than second so its no. of milliseconds should also be smaller.

You can also check it on http://www.tutorialspoint.com/cgi-binpractice.cgi?file=javascript_178

So don't know why this is behaving like this.

Any help ?

Upvotes: 2

Views: 445

Answers (2)

Deele
Deele

Reputation: 3684

Test your code if you are correctly creating Date object

// new Date(year, month, day, hour, minute, second, millisecond);

// Working with date 16/06/2014 16:50
var foo = new Date(2014, 6, 16, 16, 50);
foo.getTime(); // 1405518600000

// Working with date 01/07/2014 16:54 
var foo = new Date(2014, 7, 1, 16, 54);
foo.getTime(); // 1406901240000

Read more about Date object reference.

Until we see your code and how do you get from "16/06/2014 16:50" to "1428303000000", I can't help more.

Upvotes: 2

Wayne
Wayne

Reputation: 60414

You're probably creating the date using 16/06/2014 and intending this to mean the 16th day of the 6th month. However, this is not how it's parsed. The first element is treated as the month; the second element is the day. Since there aren't 16 months in a year, the date is rounded forward to the next year (i.e. the 16th month of 2014 is the 4th month of 2015).

In other words:

Date.parse("16/06/2014 16:50") === Date.parse("04/06/2015 16:50"); // => true

Upvotes: 2

Related Questions