-3

I need date and time in this format in javascript.

2016/02/17-14:31:36

Currently I am using

var data = new Date();

But this gives Date {Fri Feb 26 2016 18:38:45 GMT+0530 (India Standard Time)}

gfullam
  • 9,220
  • 3
  • 46
  • 59
user5798214
  • 475
  • 1
  • 5
  • 15
  • 2
    Have you tried anything? What did you find on google? – jolySoft Feb 26 '16 at 13:15
  • 7
    Possible duplicate of [Where can I find documentation on formatting a date in JavaScript?](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – MaVRoSCy Feb 26 '16 at 13:16
  • 1
    checkout this thread: http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – Armen Feb 26 '16 at 13:16
  • `data.toLocaleString()` and `date.toLocaleDateString()` might be of some help. Go to browser console and type in `var date=new Date();` and then type `date.` browser console will give you all available functions, then choose accordingly. (on Chrome) – krozaine Feb 26 '16 at 13:22

1 Answers1

0
var d = new Date(),
   year = d.getFullYear(),
   month = ('0'+(d.getMonth()+1)).slice(-2),
   day = ('0'+d.getDay()).slice(-2),
   hour = ('0'+d.getHours()).slice(-2),
   minute = ('0'+d.getMinutes()).slice(-2),
   second = ('0'+d.getSeconds()).slice(-2);

var dateString = year+'/'+month+'/'+day+'-'+hour+':'+minute+':'+second;

Next time, please google yourself. Thanks.

Edit Now it also has those leading zero's.

Christian
  • 5,910
  • 9
  • 38
  • 77