Javascript 如何获得星期几和一年中的月份?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4822852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the day of week and the month of the year?
提问by Will Marcouiller
I don't know much about Javascript, and the other questions I found are related to operations on dates, not only getting the information as I need it.
我对 Javascript 不太了解,我发现的其他问题与日期操作有关,而不仅仅是获取我需要的信息。
Objective
客观的
I wish to get the date as below-formatted:
我希望获得以下格式的日期:
Printed on Thursday, 27 January 2011 at 17:42:21
印刷于 2011 年 1 月 27 日星期四 17:42:21
So far, I got the following:
到目前为止,我得到了以下信息:
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;
I now need to know how to get the day of week and the month of year (their names).
我现在需要知道如何获得星期几和一年中的月份(他们的名字)。
Is there a simple way to make it, or shall I consider using arrays where I would simply index to the right value using now.getMonth()
and now.getDay()
?
有没有一种简单的方法来实现它,或者我应该考虑使用数组,我只需使用now.getMonth()
and索引到正确的值now.getDay()
?
回答by user113716
Yes, you'll need arrays.
是的,你需要数组。
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() ];
Or you can use the date.jslibrary.
或者您可以使用date.js库。
EDIT:
编辑:
If you're going to use these frequently, you may want to extend Date.prototype
for accessibility.
如果您要经常使用这些,您可能需要扩展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();
回答by RWC
Use the standard javascript Date class. No need for arrays. No need for extra libraries.
使用标准的 javascript Date 类。不需要数组。不需要额外的库。
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false };
var prnDt = 'Printed on ' + new Date().toLocaleTimeString('en-us', options);
console.log(prnDt);
回答by Pritam Narkhede
One thing you can also do is Extend date object to return Weekday by:
您还可以做的一件事是扩展日期对象以通过以下方式返回工作日:
Date.prototype.getWeekDay = function() {
var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return weekday[this.getDay()];
}
so, you can only call date.getWeekDay();
所以,你只能调用 date.getWeekDay();
回答by Rajesh
As @L-Rayhas already suggested, you can look into moment.jsas well
Sample
样本
var today = moment();
var result = {
day: today.format("dddd"),
month: today.format("MMM")
}
document.write("<pre>" + JSON.stringify(result,0,4) + "</pre>");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
回答by lucke84
Unfortunately, Date
object in javascript returns information about months only in numeric format. The faster thing you can do is to create an array of months (they are not supposed to change frequently!) and create a function which returns the name based on the number.
不幸的是,Date
javascript 中的对象仅以数字格式返回有关月份的信息。您可以做的更快的事情是创建一个月份数组(它们不应该经常更改!)并创建一个基于数字返回名称的函数。
Something like this:
像这样的东西:
function getMonthNameByMonthNumber(mm) {
var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
return months[mm];
}
Your code therefore becomes:
因此,您的代码变为:
var prnDt = "Printed on Thursday, " + now.getDate() + " " + getMonthNameByMonthNumber(now.getMonth) + " "+ now.getFullYear() + " at " + h + ":" + m + ":" s;
回答by P.S.S.
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;
};
回答by Phrogz
Using http://phrogz.net/JS/FormatDateTime_JS.txtyou can just:
使用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#" );
回答by Dan
回答by cambist
The toLocaleDateString()method returns a string with a language sensitive representation of the date portion of this date. The new locales and options arguments let applications specify the language whose formatting conventions should be used and allow to customize the behavior of the function. In older implementations, which ignore the locales and options arguments, the locale used and the form of the string returned are entirely implementation dependent.
所述toLocaleDateString()方法返回与这个日期的日期部分的语言敏感表示的字符串。新的语言环境和选项参数让应用程序指定应使用其格式约定的语言,并允许自定义函数的行为。在忽略语言环境和选项参数的旧实现中,使用的语言环境和返回字符串的形式完全取决于实现。
Long form:
长表:
const options = { weekday: 'long' };
const date = new Date();
console.log(date.toLocaleDateString('en-US', options));
One liner:
一个班轮:
console.log(new Date().toLocaleDateString('en-US', { weekday: 'long' }));
Note: there are other language options for locale, but the one presented here for for US English
注意:区域设置还有其他语言选项,但此处针对美国英语提供的选项
回答by Antonio Jha
That's simple. You can set option to display only week days in toLocaleDateString() to get the names. For example:
这很简单。您可以在 toLocaleDateString() 中设置选项以仅显示工作日以获取名称。例如:
(new Date()).toLocaleDateString('en-US',{ weekday: 'long'}) will return only the day of the week. And (new Date()).toLocaleDateString('en-US',{ month: 'long'}) will return only the month of the year.
(new Date()).toLocaleDateString('en-US',{ weekday: 'long'}) 将只返回星期几。并且 (new Date()).toLocaleDateString('en-US',{ month: 'long'}) 将仅返回一年中的月份。