我想知道如何在jQuery中使用Date()函数以yyyy/mm/dd格式获取当前日期。
当前回答
获取当前日期格式dd/mm/yyyy
代码如下:
var fullDate = new Date();
var twoDigitMonth = ((fullDate.getMonth().toString().length) == 1)? '0'+(fullDate.getMonth()+1) : (fullDate.getMonth()+1);
var twoDigitDate = ((fullDate.getDate().toString().length) == 1)? '0'+(fullDate.getDate()) : (fullDate.getDate());
var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
alert(currentDate);
其他回答
Moment.js让它变得非常简单:
moment().format("YYYY/MM/DD")
因为问题被标记为jQuery:
如果你也在使用jQuery UI,你可以使用$.datepicker.formatDate():
$.datepicker.formatDate('yy/mm/dd', new Date());
请看这个演示。
如果你有jQuery UI(需要datepicker),这将做的伎俩:
$.datepicker.formatDate('yy/mm/dd', new Date());
下面是获取当前日、年或月的方法
new Date().getDate() // Get the day as a number (1-31)
new Date().getDay() // Get the weekday as a number (0-6)
new Date().getFullYear() // Get the four digit year (yyyy)
new Date().getHours() // Get the hour (0-23)
new Date().getMilliseconds() // Get the milliseconds (0-999)
new Date().getMinutes() // Get the minutes (0-59)
new Date().getMonth() // Get the month (0-11)
new Date().getSeconds() // Get the seconds (0-59)
new Date().getTime() // Get the time (milliseconds since January 1, 1970)
var today = new Date(); var tday = getfullyear () + '/' + ( today.getMonth() < 9 ?'0': " ) + (today.getMonth() + 1) + '/' + ( getdate () < 10 ?'0': " ) + today.getDate(); console.log (tday);