关于javascript:如何获取星期几和一年中的某个月?

How to get the day of week and the month of the year?

我对javascript不太了解,我发现的其他问题与日期操作有关,不仅是获取我需要的信息。

目标

我希望得到以下格式的日期:

Printed on Thursday, 27 January 2011 at 17:42:21

到目前为止,我得到了以下信息:

1
2
3
4
5
6
7
8
9
10
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()简单地索引到正确的值?


是的,你需要数组。

1
2
3
4
5
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以实现可访问性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(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日期类。不需要数组。不需要额外的库。

请参阅https://developer.mozilla.org/en/docs/web/javascript/reference/global_objects/date/toLocaledateString

1
2
3
4
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
2
3
4
Date.prototype.getWeekDay = function() {
    var weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    return weekday[this.getDay()];
}

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


正如@l-ray已经建议的那样,您也可以查看moment.js

样品

1
2
3
4
5
6
7
var today = moment();
var result = {
  day: today.format("dddd"),
  month: today.format("MMM")
}

document.write("[cc lang="javascript"]" + JSON.stringify(result,0,4) +"

1
[cc lang="javascript"]<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js">


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

像这样:

1
2
3
4
5
function getMonthNameByMonthNumber(mm) {
   var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

   return months[mm];
}

因此,您的代码变为:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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;
};


使用http://phrogz.net/js/formattime_js.txt,您只需:

1
2
var now = new Date;
var prnDt = now.customFormat("Printed on #DDDD#, #D# #MMMM# #YYYY# at #hhh#:#mm#:#ss#" );

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

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

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


这很简单。您可以将选项设置为仅在toLocaledateString()中显示星期几以获取名称。例如:

(new date())。和(new date())。