JavaScript Todays date
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?
var d = new Date();
var df = d.getFullYear()
+ ('0' + String(d.getMonth()+1)).substr(-2)
+ ('0' + String(d.getDate())).substr(-2);
alert(df);
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());
链接地址: http://www.djcxy.com/p/77516.html
上一篇: 文本字段中的填充日期
下一篇: JavaScript今天的日期