如何从日期格式 'yyyy-mm-dd' 中获取日期名称?

How to get day's name from date format 'yyyy-mm-dd'?

如何通过提供日期格式 'yyyy-mm-dd' 来获取日期名称?

我有这个日期 '2015-03-03'correct format according to MDNnew Date(dateString); 构造函数。

我如何获得星期二这一天的名称?

我找到了这个问题的解决方案,所以我想与大家分享,要使用日期格式 'yyyy-mm-dd' 获取日期名称,您必须使用 javascript 文件 moment.js 并使用此函数获取当天的所有信息:

var dayInformation = String(moment('2015-03-10'));

由于时区偏移,您可能会得到错误的星期几。您可以通过将以分钟为单位的本地时区偏移量乘以一分钟的毫秒数来更正此问题。

var getDayOfWeek = (function () {
  var days = ['Sunday','Monday','Tuesday','Wednesday',
              'Thursday','Friday','Saturday'];
  return function (date) {
    return days[date.getDay()];  
  };
}());

function getLocalizedDate(dateStr) {
  var d = new Date(dateStr);
  return new Date(d.getTime() + d.getTimezoneOffset() * 60000);
}

function daysInMarch() {
    var display = document.querySelector('#display'),
        i = 1,
        dateStr, date, formatted;

    for (; i <= 31; i++) {
        dateStr = '2015-03-' + (i < 10 ? '0' + i : i),
        date = getLocalizedDate(dateStr),
        formatted = 'March ' + date.getDate() + ' 2015 was a ';
  
        display.innerHTML += formatted +  getDayOfWeek(date) + '<br />';  
    }
}

daysInMarch();
<div id="display"></div>

已经收到我昨天写的这段代码了。可能对你有用。

 /**
 * Created by jjansen on 09-Mar-15 4:56 PM.
 * Whosebug
 */
var dateName = function ( yyyy, mm, dd ) {
    var monthNames = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    ];

    var dayNames = [
        "Monday",
        "Thuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
        "Sunday"
    ];
    var today = new Date( yyyy, mm-1, dd );
    var dd = today.getDate();
    var dayname = dayNames[today.getDay()-1];
    var mm = monthNames[today.getMonth()];
    var yyyy = today.getFullYear();
    var fullDate = dayname + " " + dd + " " + mm + " " + yyyy;

    alert( "the current date is: " + fullDate );
};

dateName( 2015, 06, 02 );

你可以随心所欲地改变它并且它工作完美


http://jsfiddle.net/vuob3L16/