2013-09-21 52 views
-1

特定的日期格式我怎麼能得到這個格式在javascript:獲得在javascript

Saturday,September 21,2013 

我這樣做

var date= new Date(); 
var d= date.getDate(); 

但所有我得到的是21的願望是

一天的數

回答

0

我建議你使用一個Moment.js庫來有效處理JavaScript中的日期和時間。

var date, 
    dateString; 
date = new Date(); 
dateString = moment(date).format('dddd, MMMM, DD, YYYY'); 

DEMO

0

您可以使用以下方法:

var d = new Date(); 

d.getDate()  // returns the number of the day 
d.getMonth()  // returns the number of the month (starting from 0) 
d.getDay()  // returns the number of the day in the week 
d.getFullYear() // returns the full year, i.e. 2013 

爲了使用月份和日期的名字,你可以隨便寫一個皈依功能:

function nameOfDay(dayNumber) { 
    return [ 
     'Sunday', 
     'Monday', 
     'Tuesday', 
     'Wednesday', 
     'Thursday', 
     'Friday', 
     'Saturday' 
    ][dayNumber]; 
} 

和幾個月也是類似的。

(或者,如果該功能在您的應用程序很習慣,你可以像使用datejsMoment.js一個外部庫)