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


当前回答

//convert month to 2 digits<p>
var twoDigitMonth = ((fullDate.getMonth().length+1) === 1)? (fullDate.getMonth()+1) : '0' + (fullDate.getMonth()+1);

var currentDate =  fullDate.getFullYear()+ "/" + twoDigitMonth + "/" + fullDate.getDate();
console.log(currentDate);<br>
//2011/05/19

其他回答

关闭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 );

我只是想分享一个我用皮埃尔的想法做的时间戳原型。没有足够的点来评论:(

// US common date timestamp
Date.prototype.timestamp = function() {
  var yyyy = this.getFullYear().toString();
  var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
  var dd  = this.getDate().toString();
  var h = this.getHours().toString();
  var m = this.getMinutes().toString();
  var s = this.getSeconds().toString();

  return (mm[1]?mm:"0"+mm[0]) + "/" + (dd[1]?dd:"0"+dd[0]) + "/" + yyyy + " - " + ((h > 12) ? h-12 : h) + ":" + m + ":" + s;
};

d = new Date();

var timestamp = d.timestamp();
// 10/12/2013 - 2:04:19

供你参考——getDay()会告诉你星期几…即:如果今天是星期四,它将返回数字4(是一周的第4天)。

使用getDate()来获取一个月中的某一天。

下面是我的例子……(也是一个字符串填充函数,在单个时间元素上给出前导0。(例如:10:4:34 => 10:04:35)

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

var currentdate = new Date();
var datetime = currentdate.getDate() 
    + "/" + strpad00((currentdate.getMonth()+1)) 
    + "/" + currentdate.getFullYear() 
    + " @ " 
    + currentdate.getHours() + ":" 
    + strpad00(currentdate.getMinutes()) + ":" 
    + strpad00(currentdate.getSeconds());

示例输出:31/12/2013 @ 10:07:49如果使用getDay(),输出将是4/12/2013 @ 10:07:49

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

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

'2022-06-09'

//convert month to 2 digits<p>
var twoDigitMonth = ((fullDate.getMonth().length+1) === 1)? (fullDate.getMonth()+1) : '0' + (fullDate.getMonth()+1);

var currentDate =  fullDate.getFullYear()+ "/" + twoDigitMonth + "/" + fullDate.getDate();
console.log(currentDate);<br>
//2011/05/19