0

I am trying to get date and time in jQuery in number format for example,

yyyyMMddhhmm or you can say 201404051159

This is what I am trying,

$(document).ready(function () {
    var currentdate = new Date();
    var datetime = currentdate.getDate() + ""
                + currentdate.getHours() + ""
                + currentdate.getMinutes();

    alert(datetime);
});

http://jsfiddle.net/YN2Xs/2/

It;s not giving me right date

Mathematics
  • 7,314
  • 25
  • 77
  • 152

5 Answers5

1

There are two problems here.

First, you're using the currentdate variable without first declaring it. This is easy enough to fix by adding in var currentdate = new Date();

Second, the getter methods on a Date object don't return with leading zeros. This means you need to get the value and check if it's less than 9. If it is, you need to add in a leading zero.

Something like:

$(document).ready(function () {
  var currentdate=new Date() // Declare date variable 
  var datetime = "" + currentdate.getFullYear();
  var month = currentdate.getMonth() + 1; // Month is 0-11, not 1-12
  if (month < 10) {
    month = '0' + month;
  }
  datetime += month;
  var day = currentdate.getDate();
  if (day < 10) {
    day = '0' + day;
  }
  datetime += day;
  var hours = currentdate.getHours();
  if (hours < 10) {
    hours = '0' + hours;
  }
  datetime += hours;
  var minutes = currentdate.getMinutes();
  if (minutes < 10) {
    minutes = '0' + minutes;
  }
  datetime += minutes;
  var seconds = currentdate.getSeconds();
  if (seconds < 10) {
    seconds = '0' + seconds;
  }
  datetime += seconds;

  alert(datetime);
});
0

You missed to declare date variable

Try this

$(document).ready(function () {
        var currentdate=new Date() // Declare date variable 
        var datetime = currentdate.getDate() + ""
                    + currentdate.getHours() + ""
                    + currentdate.getMinutes() + ""
                    + currentdate.getSeconds();

        alert(datetime);
    });

DEMO

Sridhar R
  • 20,190
  • 6
  • 38
  • 35
0

You forgot to declare the date object to access date

 var date=new Date();
    var datetime = date.getDate() + ""
                + date.getHours() + ""
                + date.getMinutes() + ""
                + date.getSeconds();

Find the fiddle link:

http://jsfiddle.net/ShinyMetilda/7pHj3/1/

ShinyJos
  • 1,487
  • 11
  • 17
0

Try like this:

$(document).ready(function () {
    var currentdate = new Date();
    var datetime = currentdate.getFullYear()
                + currentdate.getMonth() 
                + currentdate.getDate()
                + currentdate.getHours()
                + currentdate.getMinutes()

    alert(datetime);
});
aksu
  • 5,221
  • 5
  • 24
  • 39
0

You could break it down like so

DEMO1 http://jsfiddle.net/YN2Xs/5/

OUTPUT1 20143593653

DEMO2 http://jsfiddle.net/YN2Xs/7/ (2 digit with zeros)

OUTPUT2 2014030595829

EXAMPLE1

$(document).ready(function () {
        var currentdate=new Date()
        var datetime = 
                    + currentdate.getFullYear() + ""
                    + currentdate.getMonth() + ""        
                    + currentdate.getDate() + ""
                    + currentdate.getHours() + ""
                    + currentdate.getMinutes() + ""
                    + currentdate.getSeconds();

    });

EXAMPLE2 with 2 digit day and month adding 0 where required

 $(document).ready(function () {
        var currentdate=new Date();
        var month = currentdate.getMonth();
        var day = currentdate.getDate();
        if(month < 10){ month = '0' + month}
        if(day < 10){ day = '0' + day}

        var datetime = 
                    + currentdate.getFullYear() + ""
                    + month + ""        
                    + day + ""
                    + currentdate.getHours() + ""
                    + currentdate.getMinutes() + ""
                    + currentdate.getSeconds();

        $('input').val(datetime);

    });

EDITED

getFullYear() replaced getYear()

Kevin Lynch
  • 24,427
  • 3
  • 36
  • 37