Search code examples
javascriptdate-formatting

Java Datestring, how to create long Day of the Week format?


This is a script that counts a desired number of business days from the present time. It works very well. I would like it to render the result as a long word day of the week, as in Monday July 06 2015, for example. The current result is Mon Jul 06 2015

I am a hacking monkey, so a dumbed-down response would not be an insult!

Number.prototype.mod = function(n) {
  return ((this % n) + n) % n;
}
Date.prototype.addBusDays = function(dd) {
  var wks = Math.floor(dd / 5);
  var dys = dd.mod(5);
  var dy = this.getDay();
  if (dy === 6 && dd > -1) {
    if (dys === 0) {
      dys -= 2;
      dy += 2;
    }
    dys++;
    dy -= 6;
  }
  if (dy === 0 && dd < 1) {
    if (dys === 0) {
      dys += 2;
      dy -= 2;
    }
    dys--;
    dy += 6;
  }
  if (dy + dys > 5) dys += 2;
  if (dy + dys < 1) dys -= 2;
  this.setDate(this.getDate() + wks * 7 + dys);
}

var today = new Date();
today.addBusDays(1);
document.getElementById('dtt').innerHTML = today.toDateString();

Usage: We use it in a full sentence:

Your package will arrive on or before <span id="dtt"></span>.

Solution

  • Just create an array of month/day names like so:

    var days = ["Sunday","Monday",...]; 
    var months = ["January", "February", ...];
    

    And then

    function formatDate(date) {
        var day = date.getDate();
        if(day < 10) {
            day = "0" + day;
        }
        return days[date.getDay()] + " " + months[date.getMonth()] + " " + day + " " + date.getFullYear();
    }
    
    console.log(formatDate(new Date()));
    

    http://jsfiddle.net/1xe39uut/2/