如何在jquery中获取当前日期? [英] How to get current date in jquery?
本文介绍了如何在jquery中获取当前日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何在jQuery中使用Date()函数以 yyyy / mm / dd
格式获取当前日期。
I want to know how to use the Date() function in jQuery to get the current date in a yyyy/mm/dd
format.
推荐答案
Date()
不属于 jQuery
,这是JavaScript的功能之一。
Date()
is not part of jQuery
, it is one of JavaScript's features.
参见关于Date对象的文档。
你可以这样做:
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var output = d.getFullYear() + '/' +
(month<10 ? '0' : '') + month + '/' +
(day<10 ? '0' : '') + day;
参见这个jsfiddle 用于证明。
代码可能看起来很复杂,因为它必须处理几个月和几个月。天数由小于 10
的数字表示(意味着字符串将有一个char而不是两个)。请参阅此jsfiddle 进行比较。
The code may look like a complex one, because it must deal with months & days being represented by numbers less than 10
(meaning the strings will have one char instead of two). See this jsfiddle for comparison.
这篇关于如何在jquery中获取当前日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文