我想知道如何在jQuery中使用Date()函数以yyyy/mm/dd格式获取当前日期。
当前回答
你可以在javascript中添加一个扩展方法。
Date.prototype.today = function () {
return ((this.getDate() < 10) ? "0" : "") + this.getDate() + "/" + (((this.getMonth() + 1) < 10) ? "0" : "") + (this.getMonth() + 1) + "/" + this.getFullYear();
}
其他回答
jQuery就是JavaScript。使用Javascript日期对象。
var d = new Date();
var strDate = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate();
这一行语句将给出YYYY-MM-DD:
new Date().toISOString().substr(0, 10)
'2022-06-09'
在JavaScript中,你可以使用date对象获取当前日期和时间;
var now = new Date();
这将获得本地客户端机器时间
jquery示例LINK
如果你正在使用jQuery DatePicker,你可以应用它在任何文本框,像这样:
$( "#datepicker" ).datepicker({dateFormat:"yy/mm/dd"}).datepicker("setDate",new Date());
我只是想分享一个我用皮埃尔的想法做的时间戳原型。没有足够的点来评论:(
// 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
看到这个。 $.now()方法是表达式(new Date). gettime()返回的数字的简写。