简体   繁体   中英

How to format the date to get the name of the day and the month?

I need to get the name of the day and the month.

This is what I am currently seeing in the page:

24, 10, 2017 17:2 PM ET

But I need to see this, for example:

Tuesday, October 24, 2017 6:00 PM ET

This is my current code:

var bindEventsToUI = function () {
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1;
    var yyyy = today.getFullYear();
    var hours = today.getHours()
    var minutes = today.getMinutes()
    var ampm;
    var $date = $('#date');

    if (hours > '11') {
        ampm = 'PM';
    } else {
        ampm = 'AM';
    }

    if(dd < 10) {
        dd = '0' + dd;
    }

    if(mm < 10) {
        mm = '0' + mm;
    }

    today = dd + ', ' + mm + ', ' + yyyy + ' ' + hours + ':' + minutes + ' ' + ampm + ' ' + 'ET';

    $date.text(today);
};

This is how you can do it with 3 lines of code instead of 30:

 var today = new Date(); var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', timeZoneName: 'short' }; console.log(new Intl.DateTimeFormat('en-US', options).format(today)); 

Internationalization API is IE11+ compatible

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM