3

Possible Duplicate:
How to get current date in JavaScript

I need to get todays date in the format yyyymmdd using JavaScript.

I've used the following code:

var d = new Date();
d.getFullYear()+ '' +((d.getMonth()+1)) + '' + d.getDate();

But the month and date are returned in single digits, does someone know how to do this?

Community
  • 1
  • 1
van
  • 8,649
  • 19
  • 58
  • 88

2 Answers2

1

You can do this following way. But you need to write one extra function as below:

function getTwoDigit(number) {       
    return (number < 10 ? '0' : '') + number       
}

Now you can use that function on your original code.

var d = new Date();d.getFullYear()+ '' +(getTwoDigit(d.getMonth()+1)) + '' + getTwoDigit(d.getDate());
ROMANIA_engineer
  • 51,252
  • 26
  • 196
  • 186
shrestha2lt8
  • 305
  • 1
  • 9
1
        var d = new Date();
        var df = d.getFullYear()
            + ('0' + String(d.getMonth()+1)).substr(-2)
            + ('0' + String(d.getDate())).substr(-2);
        alert(df);
Michael Besteck
  • 2,335
  • 16
  • 8