dateNow = new Date()
dateGet = dateNow.getDay()
document.write(dateGet.toDateString())
Why is this document.write(date11.toDateString())
not allowed in JavaScript... date11
is a variable. It gives me an error of "toDateString() is not a function"
Presuming you meant to write dateGet
and not date11
in your question:
The problem is that dateGet
is not a Date
instance-- it is a number that was returned from the getDay()
Date
method.
const dateNow = new Date() const dateGet = dateNow.getDay() console.log(dateNow instanceof Date); // true console.log(typeof dateNow.getDay); // function console.log(dateGet instanceof Date); //false console.log(typeof dateGet); // number console.log(typeof dateGet.toDateString); // undefined document.write(dateGet.toDateString()) // 🙁
Since dateGet
is not an instance of Date
but is actually a number, it has no such method as .toDateString
, so when you attempt to call this non-existent method an error is thrown.
toDateString()
is a function for Date objects. dateGet or date11 are actually integers because that's what getDay()
returns, so that's by design: toDateString() is not a function of integers . You can either `document.write(dateGet) and that'll return an integer for the day of the week. If you want "monday" or "tuesday", etc. then you'll need to create a small array of days and match to the dateGet.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.