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


当前回答

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

其他回答

使用纯Javascript,你可以创建自己的YYYYMMDD格式原型;

Date.prototype.yyyymmdd = function() {
  var yyyy = this.getFullYear().toString();
  var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
  var dd  = this.getDate().toString();
  return yyyy + "/" + (mm[1]?mm:"0"+mm[0]) + "/" + (dd[1]?dd:"0"+dd[0]); // padding
};

var date = new Date();
console.log( date.yyyymmdd() ); // Assuming you have an open console

关闭jQuery插件页面。所以手动:

function strpad00(s)
{
    s = s + '';
    if (s.length === 1) s = '0'+s;
    return s;
}

var now = new Date();
var currentDate = now.getFullYear()+ "/" + strpad00(now.getMonth()+1) + "/" + strpad00(now.getDate());
console.log(currentDate );

这是我只用jQuery得出的结果。只是把碎片拼在一起的问题。

        //Gather date information from local system
        var ThisMonth = new Date().getMonth() + 1;
        var ThisDay = new Date().getDate();
        var ThisYear = new Date().getFullYear();
        var ThisDate = ThisMonth.toString() + "/" + ThisDay.toString() + "/" + ThisYear.toString();

        //Gather time information from local system
        var ThisHour = new Date().getHours();
        var ThisMinute = new Date().getMinutes();
        var ThisTime = ThisHour.toString() + ":" + ThisMinute.toString();

        //Concatenate date and time for date-time stamp
        var ThisDateTime = ThisDate  + " " + ThisTime;

这一行语句将给出YYYY-MM-DD:

new Date().toISOString().substr(0, 10)

'2022-06-09'

这将给你当前日期字符串

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