I am developing a website for a class and I am supposed to display a countdown t
ID: 3669949 • Letter: I
Question
I am developing a website for a class and I am supposed to display a countdown to a grand opening day. In this dynamic message, I am supposed to display the day of the week, the month and year in a written format, like Saturday, Febraury 20, 2016. But my display shows "Today is 2/20/2016 2016." Here is the code that I wrote that deals with that part of the display, what is wrong?:
function todayDate() {
var today = new Date()
var dayofweek = today.toLocaleString()
dayLocate = dayofweek.indexOf(" ")
weekDay = dayofweek.substring(0, dayLocate)
newDay = dayofweek.substring(dayLocate)
dateLocate = newDay.indexOf(",")
monthDate = newDay.substring(0, dateLocate+1)
yearLocate = dayofweek.indexOf("2016")
year = dayofweek.substr(yearLocate, 4)
var expoDay = new Date("December 16, 2011")
var daysToGo = expoDay.getTime()-today.getTime()
var daysToExpo = Math.ceil(daysToGo/(1000*60*60*24))
displayDate.innerHTML = "Today is "+weekDay+" "+monthDate+" "+year+". We have "+daysToExpo+" days until the Travel Expo Opening in San Diego."
Explanation / Answer
function todayDate() {
var today = new Date()
var dayofweek = today.toLocaleString()
dayLocate = dayofweek.indexOf(" ")
weekDay = today.getDayName();
monthDay = today.getMonthName();
newDay = dayofweek.substring(dayLocate)
dateLocate = newDay.indexOf(",")
monthDate = newDay.substring(0, dateLocate+1)
yearLocate = dayofweek.indexOf("2016")
year = dayofweek.substr(yearLocate, 4)
var expoDay = new Date("December 16, 2011")
var daysToGo = expoDay.getTime()-today.getTime()
var daysToExpo = Math.ceil(daysToGo/(1000*60*60*24))
displayDate.innerHTML = "Today is "+weekDay+", "+monthDay+" "+monthDate+" "+year+". We have "+daysToExpo+" days until the Travel Expo Opening in San Diego."
}
Date.prototype.getMonthName = function() {
var m = ['January','February','March','April','May','June','July',
'August','September','October','November','December'];
return m[this.getMonth()];
}
Date.prototype.getDayName = function() {
var d = ['Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday'];
return d[this.getDay()];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.