0

I have got a task to make simple counter (it's not so simple in facts). Anyway, I can't use any plugins but jQuery.

I get an actual dateTime using

now = new Date();

I have a problem, because if I try to code

 var now = new Date();

I get this error in chrome:

    Uncaught ReferenceError: now is not defined  
at <anonymous>:2:1  
at Object.InjectedScript._evaluateOn (<anonymous>:905:140)  
at Object.InjectedScript._evaluateAndWrap (<anonymous>:838:34)  
at Object.InjectedScript.evaluate (<anonymous>:694:21) 

The output of now = Date(); is Mon Aug 31 2015 18:40:06 GMT+0200 (Stredoeurópsky čas (letný)), what is little problem, because I need to get format YYYY-MM-DD HH:ii:ss Is that possible using jQuery without any plugins? Thank you a million

  • 2
    [Duplicate](http://stackoverflow.com/questions/3066586/get-string-in-yyyymmdd-format-from-js-date-object) – sam Aug 31 '15 at 16:50

2 Answers2

0
var d = new Date,
        dformat = [d.getFullYear(),
                   d.getMonth()+1,
                   d.getDate()].join('-')+' '+
                  [d.getHours(),
                   d.getMinutes(),
                   d.getSeconds()].join(':');
Prudhvi Konda
  • 297
  • 1
  • 7
0

This may help:

function formattDate() {            
            var currentDate = new Date();
            var day = currentDate.getDate();
            var month = currentDate.getMonth()+1;
            var year = currentDate.getFullYear();
            var hours = currentDate.getHours();
            var minutes = currentDate.getMinutes();
            var seconds = currentDate.getSeconds();
            var retVal = "";
            if(day<10) {
                day = "0"+day;
            }
            if(month<10) {
                month = "0"+month;
            }           
            return day + "." + month + "." + year + ' ' + hours + ':' + minutes + ':' + seconds;
        }
Daria M
  • 222
  • 1
  • 11