Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
64 views
in Technique[技术] by (71.8m points)

javascript - New date is not an object?

I'm sorry but the new Date() is not returning an object as I understand an object.

From the MDN article on Date constructor :

When no parameters are provided, the newly-created Date object represents the current date and time as of the time of instantiation.

(emphasis added)

All right then, where is the object there :

const date1 = new Date();
console.log(date1);
// returns Wed Jan 27 2021 15:45:31 GMT+0100
question from:https://stackoverflow.com/questions/65921555/new-date-is-not-an-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When calling new Date(), you will get a date object. When calling console.log(new Date()), the toString-method of the date object will implicitly get called (not 100% true, but you get the idea). How the console-object works, is implemented by your browser/enironment. My guess is showing the date as a string is a convenience they've build in.

Like others have mentioned in the comments, try console.log(typeof new Date())/console.dir(new Date()) or to call any of the methods documented. You'll see it is most definitely an object.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...