2013-09-21 51 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一个外部库)