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


当前回答

使用纯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 UI(需要datepicker),这将做的伎俩:

$.datepicker.formatDate('yy/mm/dd', new Date());
console.log($.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 d = new Date();

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

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

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

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

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