0

I am trying to change the subtract date format by this javascript code but i want it should print day name for example "Sunday". currently it's printing Wed "Jan 30 2019 02:31:30 GMT+0600 (Bangladesh Standard Time)"

var el_up = document.getElementById("GFG_UP");
var el_down = document.getElementById("GFG_DOWN");
var today = new Date();
el_up.innerHTML = "Today's date = " + today;

Date.prototype.subtractDays = function(d) {
  this.setTime(this.getTime() - (d * 24 * 60 * 60 * 1000));
  return this;
}

function gfg_Run() {
  var a = new Date();
  a.subtractDays(4);
  el_down.innerHTML = a;
}
<h1 style="color:green;">
  GeeksForGeeks
</h1>

<p id="GFG_UP" style="font-size: 15px; font-weight: bold;">
</p>

<button onclick="gfg_Run()"> 
       subtractDays 
      </button>

<p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;">
</p>

How can I convert it?

j08691
  • 204,283
  • 31
  • 260
  • 272
Md. Elias
  • 3
  • 6
  • 1
    Simply run `.getDay();` on `new Date()` to get the day. Like this: `new Date().getDay();` Then simply map the day number to an array of the weekdays – mufasa Jan 29 '20 at 20:36
  • Thanks for your reply @mufasa here is the map of the day number `function myFunction() { var d = new Date(); var weekday = new Array(7); weekday[0] = "Sunday"; weekday[1] = "Monday"; weekday[2] = "Tuesday"; weekday[3] = "Wednesday"; weekday[4] = "Thursday"; weekday[5] = "Friday"; weekday[6] = "Saturday"; var n = weekday[d.getDay()]; document.getElementById("demo").innerHTML = n; }` but subtract is not working. would you pls show me how subtract work. i wand 4 days Subtract Thanks – Md. Elias Jan 30 '20 at 05:43
  • I found a way... `

    `
    – Md. Elias Jan 30 '20 at 06:41

1 Answers1

0

You need to extract the day with getDay() method. You will get the day as number, so if you want to get the name you should compare it to an array.

Example:

const d = new Date();
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
console.log(days[d.getDay()]);
A. Meshu
  • 4,053
  • 2
  • 20
  • 34