admin管理员组

文章数量:1195141

I was trying to convert date object into long format (may be in milliseconds format) as we do in java.
So to fulfill my need, after some trial and error, I found below way which works for me:

var date = new Date();  
var longFormat = date*1;  // dont know what it does internally
console.log(longFormat); // output was 1380625095292  

To verify, I reverse it using new Date(longFormat); and it gave me correct output. In short I was able to fulfill my need some how, but I am still blank what multiplication does internally ? When I tried to multiply current date with digit 2, it gave me some date of year 2057 !! does anyone know, what exactly happening ?

I was trying to convert date object into long format (may be in milliseconds format) as we do in java.
So to fulfill my need, after some trial and error, I found below way which works for me:

var date = new Date();  
var longFormat = date*1;  // dont know what it does internally
console.log(longFormat); // output was 1380625095292  

To verify, I reverse it using new Date(longFormat); and it gave me correct output. In short I was able to fulfill my need some how, but I am still blank what multiplication does internally ? When I tried to multiply current date with digit 2, it gave me some date of year 2057 !! does anyone know, what exactly happening ?

Share Improve this question asked Oct 1, 2013 at 11:08 AGuptaAGupta 5,73410 gold badges56 silver badges92 bronze badges 3
  • 3 Please read the manual ... this is the number of milliseconds since 1/1/1970 (equivalent to date.getTime()) – devnull69 Commented Oct 1, 2013 at 11:11
  • @devnull69 could you please share some resource link which can clarify my doubts? – AGupta Commented Oct 1, 2013 at 11:15
  • 1 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… might be a good start – devnull69 Commented Oct 1, 2013 at 11:17
Add a comment  | 

6 Answers 6

Reset to default 12

The long format displays the number of ticks after 01.01.1970, so for now its about 43 years.

* operator forces argument to be cast to number, I suppose, Date object has such casting probably with getTime().

You double the number of milliseconds - you get 43 more years, hence the 2057 (or so) year.

What you are getting when you multiply, is ticks

Visit: How to convert JavaScript date object into ticks

Also, when you * 2 it, you get the double value of ticks, so the date is of future

var date = new Date()
var ticks = date.getTime()

ref: Javascript Date Ticks

getTime returns the number of milliseconds since January 1, 1970. So when you * 1 it, you might have got value of this milliseconds. When you * 2 it, those milliseconds are doubled, and you get date of 2057!!

Javascript date objects are based on a UTC time value that is milliseconds since 1 January 1970. It just so happens that Java uses the same epoch but the time value is seconds.

To get the time value, the getTime method can be used, or a mathematic operation can be applied to the date object, e.g.

var d = new Date();
alert(d.getTime());  // shows time value
alert(+d);           // shows time value

The Date constructor also accepts a time value as an argument to create a date object, so to copy a date object you can do:

var d2 = new Date(+d);

If you do:

var d3 = new Date(2 * d);

you are effectively creating a date that is (very roughly):

1970 + (2013 - 1970) * 2 = 2056

Dates are internally stored as a timestamp, which is a long-object (more info on timestamps). This is why you can create Dates with new Date(long). If you try to multiply a Date with an integer, this is what happens:

    var date = new Date();
    var longFormat = date*1;
    // date*1 => date.getTime() * 1

    console.log(longFormat); // output is 1380.....

Javascript tries to find the easiest conversion from date to a format that can be multiplied with the factor 1, which is in this case the internal long format

Just use a date object methods.

Read the docs: JavaScript Date object

var miliseconds=yourDateObject.getMiliseconds();

If You want to get ticks:

var ticks = ((yourDateObject.getTime() * 10000) + 621355968000000000);

or

var ticks = someDate.getTime();

You could try the parsing functionality of the Date constructor, whose result you then can stringify:

>

 new Date("04/06/13").toString()
"Sun Apr 06 1913 00:00:00 GMT+0200"

// or something But the parsing is implementation-dependent, and there won't be many engines that interpret your odd DD/MM/YY format correctly. If you had used MM/DD/YYYY, it probably would be recognized everywhere.

Instead, you want to ensure how it is parsed, so have to do it yourself and feed the single parts into the constructor:

var parts = "04/06/13".split("/"),
    date = new Date(+parts[2]+2000, parts[1]-1, +parts[0]);
console.log(date.toString()); // Tue Jun 04 2013 00:00:00 GMT+0200

本文标签: javascriptConversion of date into long formatHow it worksStack Overflow