Deepak Keynes
Deepak Keynes

Reputation: 311

How to convert a date to a string in JavaScript?

<script>
var tdt= new Date();
t=tdt.getFullYear()+"-"+(tdt.getMonth()+1)+"-"+tdt.getDate() // 2015-04-17
var tody = new Date(t).getTime(); /* 1429209000000 */ 
document.write(tody);
<script>

When I execute this code in Mozilla Firefox, I get 'NaN', but when I execute this code in Google Chrome, I get '1429209000000'. Does anyone know how to get the value '1429209000000' to be printed when I execute in Firefox and Opera?

Upvotes: 2

Views: 77

Answers (1)

Anarion
Anarion

Reputation: 1038

Just use slashes as separators.

var tdt= new Date();
t=tdt.getFullYear()+"/"+(tdt.getMonth()+1)+"/"+tdt.getDate() // 2015-04-17
var tody = new Date(t).getTime(); /* 1429209000000 */ 
document.write(tody);

Upvotes: 3

Related Questions