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()和now.getDay()将其索引到正确的值?
8 个解决方案
#1
183
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.js library.
或者您可以使用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();
#2
6
As @L-Ray has already suggested, you can look into moment.js as well
正如@ L-Ray已经建议的那样,你也可以查看moment.js
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>
#3
5
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.
不幸的是,javascript中的Date对象仅以数字格式返回有关月份的信息。您可以做的更快的事情是创建一个月数组(它们不应经常更改!)并创建一个函数,该函数根据数字返回名称。
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;
#4
5
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();
#5
3
Using http://phrogz.net/JS/FormatDateTime_JS.txt you 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#" );
#6
3
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;
};
#7
2
You can look at datejs which parses the localized date output for example.
例如,您可以查看解析本地化日期输出的datejs。
The formatting may look like this, in your example:
在您的示例中,格式可能如下所示:
new Date().toString('dddd, d MMMM yyyy at HH:mm:ss')
#8
1
Use the standard javascript Date class. No need for arrays. No need for extra libraries.
使用标准的javascript Date类。不需要数组。无需额外的库。
请参阅https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
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);
#1
183
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.js library.
或者您可以使用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();
#2
6
As @L-Ray has already suggested, you can look into moment.js as well
正如@ L-Ray已经建议的那样,你也可以查看moment.js
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>
#3
5
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.
不幸的是,javascript中的Date对象仅以数字格式返回有关月份的信息。您可以做的更快的事情是创建一个月数组(它们不应经常更改!)并创建一个函数,该函数根据数字返回名称。
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;
#4
5
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();
#5
3
Using http://phrogz.net/JS/FormatDateTime_JS.txt you 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#" );
#6
3
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;
};
#7
2
You can look at datejs which parses the localized date output for example.
例如,您可以查看解析本地化日期输出的datejs。
The formatting may look like this, in your example:
在您的示例中,格式可能如下所示:
new Date().toString('dddd, d MMMM yyyy at HH:mm:ss')
#8
1
Use the standard javascript Date class. No need for arrays. No need for extra libraries.
使用标准的javascript Date类。不需要数组。无需额外的库。
请参阅https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
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);