简体   繁体   English

如何格式化日期以获取日期和月份的名称?

[英]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: 这是使用3行代码而不是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 国际化API与IE11 +兼容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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