-5

I'm having a hard time trying to figure this out

$datenow = date('Y-m-j H:i:s');  // 2014-08-19 17:56:13

I would like to generate the exact date format with JS, how I could do this?

This is how it should appears: 2014-08-19 19:57:59

Alfonso
  • 3
  • 1
  • 3

1 Answers1

1

I would go like this :)

date = new Date();

dateFormated = date.getFullYear() + '-' + (date.getMonth()+1) + '-' +
date.getDay() + ' ' + date.getHours() + ':' + date.getMinutes() + ':'
 + date.getSeconds();

http://jsfiddle.net/r5mdggc8/2/

There are no leading zeros, but you can add them by using condition on each "date item" like so:

dateItem = getDay();
if (dateItem.toString().length < 2) { 
   dateItem = '0' + dateItem;
}

Of course, you can make a function out of it.

Brad
  • 146,404
  • 44
  • 300
  • 476
Lukáš Řádek
  • 1,014
  • 1
  • 9
  • 18