先new一個Date物件, 再用它去取得現在日期和時間


要注意的是getMonth()取回來的月份是0-11, 所以還要自行加一

另外前面補0的方法可參照下面範例, 十分簡潔, 可以寫在一行裡面

--

轉自 http://stackoverflow.com/questions/8398897/how-to-get-current-date-in-jquery

 

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

See the documentation on Date object.

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.

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.

--

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 dizzy03 的頭像
    dizzy03

    碎碎念

    dizzy03 發表在 痞客邦 留言(0) 人氣()