我想知道如何在jQuery中使用Date()函数以yyyy/mm/dd格式获取当前日期。


当前回答

你可以这样做:

    var now = new Date();
    dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
     // Saturday, June 9th, 2007, 5:46:21 PM

或者类似的东西

    var dateObj = new Date();
    var month = dateObj.getUTCMonth();
    var day = dateObj.getUTCDate();
    var year = dateObj.getUTCFullYear();
    var newdate = month + "/" + day + "/" + year;
    alert(newdate);

其他回答

var d = new Date();

var today = d.getFullYear() + '/' + ('0'+(d.getMonth()+1)).slice(-2) + '/' + ('0'+d.getDate()).slice(-2);

你可以在javascript中添加一个扩展方法。

Date.prototype.today = function () {
    return ((this.getDate() < 10) ? "0" : "") + this.getDate() + "/" + (((this.getMonth() + 1) < 10) ? "0" : "") + (this.getMonth() + 1) + "/" + this.getFullYear();
}

我知道我迟到了,但这就是你所需要的

var date = (new Date()).toISOString().split('T')[0];

toISOString()使用javascript的内置函数。

cd = (new Date()).toISOString().split('T')[0]; console.log (cd); 警报(cd);

jQuery就是JavaScript。使用Javascript日期对象。

var d = new Date();
var strDate = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate();

因为问题被标记为jQuery:

如果你也在使用jQuery UI,你可以使用$.datepicker.formatDate():

$.datepicker.formatDate('yy/mm/dd', new Date());

请看这个演示。