Wil*_*ler 110 javascript datetime dayofweek
我对Javascript了解不多,我发现的其他问题与日期操作有关,不仅仅是根据需要获取信息.
我希望得到以下格式的日期:
于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;
Run Code Online (Sandbox Code Playgroud)
我现在需要知道如何获得星期几和一年中的某个月(他们的名字).
有没有一种简单的方法来制作它,或者我是否应该考虑使用数组,我只需使用now.getMonth()
和索引到正确的值now.getDay()
?
use*_*716 209
是的,你需要数组.
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() ];
Run Code Online (Sandbox Code Playgroud)
或者您可以使用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();
Run Code Online (Sandbox Code Playgroud)
RWC*_*RWC 29
使用标准的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);
Run Code Online (Sandbox Code Playgroud)
Muh*_*red 17
我认为这是我找到的最好的解决方案
new Date(payload.value).toLocaleString("en", { weekday: "long" })
Run Code Online (Sandbox Code Playgroud)
小智 8
您还可以做的一件事是将日期对象扩展为返回工作日:
Date.prototype.getWeekDay = function() {
var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return weekday[this.getDay()];
}
Run Code Online (Sandbox Code Playgroud)
所以,你只能调用date.getWeekDay();
小智 6
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;
};
Run Code Online (Sandbox Code Playgroud)
var today = moment();
var result = {
day: today.format("dddd"),
month: today.format("MMM")
}
document.write("<pre>" + JSON.stringify(result,0,4) + "</pre>");
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
不幸的是,Date
javascript中的对象仅以数字格式返回有关月份的信息.您可以做的更快的事情是创建一个月数组(它们不应经常更改!)并创建一个函数,该函数根据数字返回名称.
像这样的东西:
function getMonthNameByMonthNumber(mm) {
var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
return months[mm];
}
Run Code Online (Sandbox Code Playgroud)
因此,您的代码变为:
var prnDt = "Printed on Thursday, " + now.getDate() + " " + getMonthNameByMonthNumber(now.getMonth) + " "+ now.getFullYear() + " at " + h + ":" + m + ":" s;
Run Code Online (Sandbox Code Playgroud)
小智 5
所述toLocaleDateString()方法返回与这个日期的日期部分的语言敏感表示的字符串。新的语言环境和选项参数让应用程序指定应使用其格式约定的语言,并允许自定义函数的行为。在忽略语言环境和选项参数的旧实现中,使用的语言环境和返回字符串的形式完全取决于实现。
长表:
const options = { weekday: 'long' };
const date = new Date();
console.log(date.toLocaleDateString('en-US', options));
Run Code Online (Sandbox Code Playgroud)
一个班轮:
console.log(new Date().toLocaleDateString('en-US', { weekday: 'long' }));
Run Code Online (Sandbox Code Playgroud)
注意:区域设置还有其他语言选项,但此处针对美国英语提供的选项
归档时间: |
|
查看次数: |
179795 次 |
最近记录: |