-1

I have been trying to display the following "on this 4th day of January in the year 2022"

using javascript on an HTML page

const monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];
const d = new Date();
var dayn = d.getDay()
var dayString;
if (dayn == 1) {
  dayString = "</b><sup>st</sup>"
} else if (dayn == 3) {
  dayString = "</b><sup>rd</sup>"
} else if (dayn >= 4) {
  dayString = "</b><sup>th</sup>"
} else {
  dayString = "</b><sup>nd</sup>"
}
var dateStr = "<b>" + dayn + dayString + "</b> day of<b> " + monthNames[d.getMonth()] + "</b> in the year <b>" + d.getFullYear(); + "</b>"
document.write(dateStr);

but for some reason it is displaying today as the 2nd and not the 4th and

where I want to display ie. 4th March 2022 (today +89 days) it is failing to do so...

So the aim is to on the first line display:

"on this 4th day of January in the year 2022"

and on the next line

"no later than 3rd day of March 2022"

Rajesh
  • 24,354
  • 5
  • 48
  • 79
Staffie
  • 3
  • 5
  • `var dayn = d.getDay()` should be `var dayn = d.getDate()` Day refers to Day of Week. Since its Tuesday, it shows 2. – Rajesh Jan 04 '22 at 06:44
  • FANTASTIC Rajesh, Thanks! If I want to do the second part, today +89 days? Can I create a second var? – Staffie Jan 04 '22 at 06:51
  • Hi @Rajesh & Shibran Thank you for assisting so far. Being a complete NOOB at this I have one more question being how do I use the JS in two places, with two different results on one HTML page as it does not seem to work where I tried it... :-( So, firstly I want to display "on this 4th day of January in the year 2022" - today's day/date and on the next line "no later than 3rd day of March 2022" - being 88 days after today's date When I ran the two scripts on one page only the first one displayed which tells me the code can't be re-used? – Staffie Jan 04 '22 at 10:16
  • @Staffie—if you have another question, post another question. Don't ask questions in comments. – RobG Jan 05 '22 at 00:13

2 Answers2

2

You have to use d.getDate() instead of d.getDay()

const monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];
const d = new Date();
var dayn = d.getDate();
var endDate = d.setDate(dayn + 89);
var daym = new Date(endDate).getDate();

var dayString;
if (dayn == 1) {
  dayString = "</b><sup>st</sup>"
} else if (dayn == 3) {
  dayString = "</b><sup>rd</sup>"
} else if (dayn >= 4) {
  dayString = "</b><sup>th</sup>"
} else {
  dayString = "</b><sup>nd</sup>"
}

var endDayString;
if (daym == 1) {
  endDayString = "</b><sup>st</sup>"
} else if (daym == 3) {
  endDayString = "</b><sup>rd</sup>"
} else if (daym >= 4) {
  endDayString = "</b><sup>th</sup>"
} else {
  endDayString = "</b><sup>nd</sup>"
}
 
var endDateStr = "<b>" + daym + endDayString + "</b> day of<b> " + monthNames[d.getMonth()] + "</b> in the year <b>" + d.getFullYear(); + "</b>"
document.write(endDateStr);
Shibran
  • 21
  • 4
0
const monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];
const d = new Date();
var dayn = d.getDate();
var dayString;
if (dayn == 1) {
  dayString = "</b><sup>st</sup>"
} else if (dayn == 3) {
  dayString = "</b><sup>rd</sup>"
} else if (dayn >= 4) {
  dayString = "</b><sup>th</sup>"
} else {
  dayString = "</b><sup>nd</sup>"
}
var dateStr = "<b>" + dayn + dayString + "</b> day of<b> " + monthNames[d.getMonth()] + "</b> in the year <b>" + d.getFullYear(); + "</b>"
document.write(dateStr);
Hardik Trada
  • 158
  • 1
  • 4