如何获得星期几和一年中的哪一天?

我不太了解Javascript,而我发现的其他问题与date操作有关,不仅仅是获取所需的信息。

目的

我希望得到如下格式的date:

于2011年1月27日星期四17:42:21印刷

到目前为止,我得到了以下几点:

var now = new Date(); var h = now.getHours(); var m = now.getMinutes(); var s = now.getSeconds(); h = checkTime(h); m = checkTime(m); s = checkTime(s); var prnDt = "Printed on Thursday, " + now.getDate() + " January " + now.getFullYear() + " at " + h + ":" + m + ":" s; 

我现在需要知道如何得到星期几和一年中的月份(他们的名字)。

是否有一个简单的方法来做到这一点,或者我应该考虑使用数组,我会简单地使用now.getMonth()now.getDay()索引到正确的值?

是的,你需要数组。

 var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; var months = ['January','February','March','April','May','June','July','August','September','October','November','December']; var day = days[ now.getDay() ]; var month = months[ now.getMonth() ]; 

或者你可以使用date.js库。


编辑:

如果你要经常使用这些,你可能需要扩展Date.prototype的可访问性。

 (function() { var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; var months = ['January','February','March','April','May','June','July','August','September','October','November','December']; Date.prototype.getMonthName = function() { return months[ this.getMonth() ]; }; Date.prototype.getDayName = function() { return days[ this.getDay() ]; }; })(); var now = new Date(); var day = now.getDayName(); var month = now.getMonthName(); 

不幸的是,JavaScript中的Date对象只返回数字格式的月份信息。 你可以做的更快的事情是创build一个数组(它们不应该经常改变!),并创build一个函数,返回基于数字的名字。

像这样的东西:

 function getMonthNameByMonthNumber(mm) { var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); return months[mm]; } 

你的代码因此变成:

 var prnDt = "Printed on Thursday, " + now.getDate() + " " + getMonthNameByMonthNumber(now.getMonth) + " "+ now.getFullYear() + " at " + h + ":" + m + ":" s; 

你还可以做的一件事是扩展date对象返回平日:

 Date.prototype.getWeekDay = function() { var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; return weekday[this.getDay()]; } 

所以,你只能调用date.getWeekDay();

正如@ L-Ray已经build议的那样,你也可以看看moment.js

样品

 var today = moment(); var result = { day: today.format("dddd"), month: today.format("MMM") } document.write("<pre>" + JSON.stringify(result,0,4) + "</pre>"); 
 <script src="ajax/libs/moment.js/2.13.0/moment.min.js"></script> 

使用http://phrogz.net/JS/FormatDateTime_JS.txt你可以:;

 var now = new Date; var prnDt = now.customFormat( "Printed on #DDDD#, #D# #MMMM# #YYYY# at #hhh#:#mm#:#ss#" ); 
 var GetWeekDays = function (format) { var weekDays = {}; var curDate = new Date(); for (var i = 0; i < 7; ++i) { weekDays[curDate.getDay()] = curDate.toLocaleDateString('ru-RU', { weekday: format ? format : 'short' }); curDate.setDate(curDate.getDate() + 1); } return weekDays; }; me.GetMonthNames = function (format) { var monthNames = {}; var curDate = new Date(); for (var i = 0; i < 12; ++i) { monthNames[curDate.getMonth()] = curDate.toLocaleDateString('ru-RU', { month: format ? format : 'long' }); curDate.setMonth(curDate.getMonth() + 1); } return monthNames; }; 

例如,您可以查看parsing本地化date输出的datejs 。

格式可能看起来像这样,在你的例子中:

 new Date().toString('dddd, d MMMM yyyy at HH:mm:ss')