Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

javascript - 如何在jquery中获取当前日期?(How to get current date in jquery?)

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

  ask by Sara translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Date() is not part of jQuery , it is one of JavaScript's features.

(Date()不是jQuery一部分,它是JavaScript的一个特性。)

See the documentation on Date object .

(请参阅Date对象的文档 。)

You can do it like that:

(你可以这样做:)

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;

See this jsfiddle for a proof.

(请参阅此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).

(代码可能看起来很复杂,因为它必须处理由小于10的数字表示的月和日(意味着字符串将有一个char而不是两个)。)

See this jsfiddle for comparison.

(请参阅此jsfiddle进行比较。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...