我想知道如何在jQuery中使用Date()函数以yyyy/mm/dd格式获取当前日期。
当前回答
供你参考——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
其他回答
jQuery就是JavaScript。使用Javascript日期对象。
var d = new Date();
var strDate = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate();
看到这个。 $.now()方法是表达式(new Date). gettime()返回的数字的简写。
在JavaScript中,你可以使用date对象获取当前日期和时间;
var now = new Date();
这将获得本地客户端机器时间
jquery示例LINK
如果你正在使用jQuery DatePicker,你可以应用它在任何文本框,像这样:
$( "#datepicker" ).datepicker({dateFormat:"yy/mm/dd"}).datepicker("setDate",new Date());
供你参考——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
var today = new Date(); var tday = getfullyear () + '/' + ( today.getMonth() < 9 ?'0': " ) + (today.getMonth() + 1) + '/' + ( getdate () < 10 ?'0': " ) + today.getDate(); console.log (tday);