Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of date into long format, How it works?

Tags:

javascript

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 ?

like image 830
A Gupta Avatar asked Oct 01 '13 11:10

A Gupta


People also ask

What is date in long format?

For example, you can combine the General Date and Long Time formats as follows: m/dd/yyyy h:mm:ss.

How do you convert DateTime to mm dd yyyy?

For the data load to convert the date to 'yyyymmdd' format, I will use CONVERT(CHAR(8), TheDate, 112). Format 112 is the ISO standard for yyyymmdd.


2 Answers

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.

like image 67
Artyom Neustroev Avatar answered Nov 20 '22 21:11

Artyom Neustroev


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!!

like image 41
Paritosh Avatar answered Nov 20 '22 20:11

Paritosh