0

How can I get the day's name by giving the date format 'yyyy-mm-dd' ?

I have this date '2015-03-03' which is in the correct format according to MDN for the new Date(dateString); constructor.

How do I get the day's name which is Tuesday?

robbmj
  • 16,085
  • 8
  • 38
  • 63
Wissem Achour
  • 177
  • 1
  • 3
  • 13

3 Answers3

1

I found the solution of this question so i wanted to share with it you guys,to get the day's name using the date format 'yyyy-mm-dd' you have to use the javascript file moment.js and use this function to get all the day's information:

var dayInformation = String(moment('2015-03-10'));
Wissem Achour
  • 177
  • 1
  • 3
  • 13
1

You are likely getting the wrong day of the week because of time zone off sets. You can correct this by multiplying the local time-zone offset in minutes by the number of milliseconds in a minute.

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>
robbmj
  • 16,085
  • 8
  • 38
  • 63
1

Already got this snippet that I wrote yesterday. Might be usefull for you.

 /**
 * Created by jjansen on 09-Mar-15 4:56 PM.
 * stackoverflow
 */
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 );

You can change this any way you want and it works flawless


http://jsfiddle.net/vuob3L16/
jimmy jansen
  • 647
  • 4
  • 12