0

Possible Duplicate:
how to display a date as 2/25/2007 format in javascript, if i have date object

How can I format the following date+time to this one 2012-07-24 17:00 ?

http://jsfiddle.net/28Tgz/1/

I am trying to make use of

formatDate('yy-mm-dd HH:ii', now)); 

without luck.

jQuery(document).ready(function() {
var foo = jQuery('#foo');

function updateTime() {
    var now = new Date();
    foo.val(now.toString());        
}

updateTime();
setInterval(updateTime, 5000); // 5 * 1000 miliseconds
});

this return me Wed Jul 25 2012 17:14:02 GMT+0300 (GTB Daylight Time)

Community
  • 1
  • 1
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179

2 Answers2

0
jQuery(document).ready(function() {
    var foo = jQuery('#foo');

    function updateTime() {
        var now = new Date();
        var date = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate() + ' ' + now.getHours() + ':' + now.getMinutes(); 
        foo.val(date);        
    }

    updateTime();
    setInterval(updateTime, 5000); // 5 * 1000 miliseconds
});

Demo

You can pad 0 if date/month/hour/minutes is less than 10.

Jashwant
  • 28,410
  • 16
  • 70
  • 105
0

This should get it done:

function updateTime() {
    var now = new Date(),
        d = [];

    d[0] = now.getFullYear().toString(),
    d[1] = now.getMonth()+1, //months are 0-based
    d[2] = now.getDate(),
    d[3] = now.getHours(),
    d[4] = now.getMinutes();

    //doing YY manually as getYear() is deprecated
    //remove the next line if you want YYYY instead of YY
    d[0] = d[0].substring(d[0].length-2); //not using substr(-2) as it doesn't work in IE

    //leading zeroes
    for (var i=1; i<=4; i++)
        if (d[i] < 10) d[i] = '0' + d[i];

    foo.val(d[0] + '-' + d[1] + '-' + d[2] + ' ' + d[3] + ':' + d[4]);
}

Fiddle

Reference

MDN Javascript Date Object
10 ways to format time and date using JavaScript
Formatting a date in JavaScript

Community
  • 1
  • 1
Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
  • Thank you for this. I find it strange that I can't get it working into my page though... – EnexoOnoma Jul 25 '12 at 15:17
  • Did you copy the whole function? – Fabrício Matté Jul 25 '12 at 15:18
  • Yes entirely. Is it because I have other jQuery scripts too ? – EnexoOnoma Jul 25 '12 at 15:21
  • Very unlikely, seeing as my script uses local variables only. Well, give a try to this other [fiddle](http://jsfiddle.net/ult_combo/28Tgz/58/) even though it's pretty much the same but with `.push` instead of creating the indexes. I'd say to check your javascript console for errors (F12 in Chrome and FF with Firebug installed, or Ctrl+Shift+K for Firefox's native console), now if that still doesn't work then your best bet is [moment.js](http://momentjs.com/) – Fabrício Matté Jul 25 '12 at 15:32
  • Found my error. I have accepted your answer. Thank you very much! – EnexoOnoma Jul 25 '12 at 15:33
  • I have just noticed that the date is wrong, as in here http://jsfiddle.net/ult_combo/28Tgz/57/ the date is 03 instead of 25 – EnexoOnoma Jul 25 '12 at 15:35
  • Oh wow, good catch. Gimme a second to review it. – Fabrício Matté Jul 25 '12 at 15:40
  • Alright, fixed. I accidentally typed the day of week instead of day of the month. Just swap `now.getDay()` by `now.getDate()` -- already fixed in the code and fiddle. Yeah about time to call it a night. `:P` – Fabrício Matté Jul 25 '12 at 15:42