如何得到星期几和一年中的月份?

我对 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;

我现在需要知道如何获取星期的日期和年的月份(他们的名字)。

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

299912 次浏览

是的,您需要数组。

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();

使用 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#" );

不幸的是,javascript 中的 Date对象仅以数字格式返回关于月份的信息。您可以做的最快的事情是创建一个月数组(它们不应该经常更改!)并创建一个函数,该函数根据数字返回名称。

就像这样:

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;

例如,您可以查看解析本地化日期输出的 约会

在您的示例中,格式可能如下所示:

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

还可以通过下面的方式将 date 对象扩展为返回 Weekday:

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

因此,您只能调用 date.getWeekDay () ;

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;
};

正如@已经建议的那样,您也可以查看 等一下 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="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>

使用标准的 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);

这很简单。您可以在 toLocaleDateString ()中将选项设置为仅显示工作日来获取名称。例如:

(新日期())。ToLocaleDateString (‘ en-US’,{ week day: ‘ long’})将只返回一周中的某一天。及(新日期())。ToLocaleDateString (‘ en-US’,{ month: ‘ long’})将只返回一年中的月份。

来自 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

ToLocaleDateString ()方法返回一个字符串,该字符串具有此日期的日期部分的语言敏感表示形式。新的区域设置和选项参数允许应用程序指定应该使用其格式化约定的语言,并允许自定义函数的行为。在较旧的实现中(忽略 locale 和 options 参数) ,所使用的 locale 和返回的字符串的形式完全依赖于实现。

长格式:

const options = { weekday: 'long' };
const date = new Date();
console.log(date.toLocaleDateString('en-US', options));

一句话:

console.log(new Date().toLocaleDateString('en-US', { weekday: 'long' }));

注意: 对于 locale 还有其他语言选项,但是对于美国英语,这里提供了一个选项

  function currentDate() {
var monthNames = [ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ];
var days = ['SUNDAY','MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY'];
var today = new Date();
var dd   = today.getDate();
var mm   = monthNames[today.getMonth()];
var yyyy = today.getFullYear();
var day  = days[today.getDay()];
today = 'Date is :' + dd + '-' + mm + '-' + yyyy;
document.write(today +"<br>");
document.write('Day is : ' + day );
}
currentDate();

写日期 = <script> document.write(Date()); </script>

我觉得这是我找到的最好的解决办法

new Date(payload.value).toLocaleString("en", { weekday: "long" })

我们可以使用下面的代码片段,如果一周从1: ‘ Monday’,2: ‘ Monday’,3: ‘ Sunday’,4: ‘ Sunday’,5: ‘ Friday’,6: ‘ Saturday’,7: ‘ Sunday’开始。

let weekDays = {
1: 'Monday',
2: 'Tuesday',
3: 'Wednesday',
4: 'Thursday',
5: 'Friday',
6: 'Saturday',
7:'Sunday'};
return weekDays[dt];
new Date().toUTCString()

它将字符串的格式设置为: Tue, 31 Dec 2024 23:59:59 GMT Https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/date/toutcstring