-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
  • What does this Format Looks like? show what you want – Mauricio Piber Fão Aug 19 '14 at 17:55
  • Straight JavaScript? Or are you using a date formatting library? – Brad Aug 19 '14 at 17:55
  • please search before you post a question in here .. http://stackoverflow.com/questions/3552461/how-to-format-javascript-date – Udaya Sri Aug 19 '14 at 17:56
  • What's a JS date format? There are many formats that JavaScript understands, the simplest of them is milliseconds since epoch. How are you going to use it? – Ruan Mendes Aug 19 '14 at 17:57
  • http://blog.stevenlevithan.com/archives/date-time-format <-- provides a list of javascript date formatting options as well as code. Please make an effort to provide some example code/what you've tried before posting. – drmarvelous Aug 19 '14 at 17:58

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
  • 159,648
  • 54
  • 349
  • 530
Lukáš Řádek
  • 1,273
  • 1
  • 11
  • 23