How to get the day of week and the month of the ye

2019-01-02 20:20发布

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.

Objective

I wish to get the date as below-formatted:

Printed on Thursday, 27 January 2011 at 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()?

9条回答
有味是清欢
2楼-- · 2019-01-02 20:55

As @L-Ray has already suggested, you can look into moment.js as well

Sample

var today = moment();
var result = {
  day: today.format("ffffdd"),
  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楼-- · 2019-01-02 20:56

Using http://phrogz.net/JS/FormatDateTime_JS.txt you can just:

var now = new Date;
var prnDt = now.customFormat( "Printed on #DDDD#, #D# #MMMM# #YYYY# at #hhh#:#mm#:#ss#" );
查看更多
初与友歌
4楼-- · 2019-01-02 20:58

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.

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;
查看更多
爱死公子算了
5楼-- · 2019-01-02 20:59

That's simple. You can set option to display only week days in toLocaleDateString() to get the names. For example:

(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.

查看更多
余生无你
6楼-- · 2019-01-02 21:01

You can look at datejs which parses the localized date output for example.

The formatting may look like this, in your example:

new Date().toString('ffffdd, d MMMM yyyy at HH:mm:ss') 
查看更多
裙下三千臣
7楼-- · 2019-01-02 21:05
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;
};
查看更多
登录 后发表回答