Why is the following expression invalid?
var hello = new Date(2010, 11, 17, 0, 0, 0, 0);
For example, hello.UTC() doesn’t work.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
and
These two are actually two different functions that print out different things.
toUTCString()– Converts a Date object to a string, according to universal timewhere as
Date.UTC()– returns the number of milliseconds in a date string since midnight of January 1, 1970, according to universal time.If you are attempting to calculate the milliseconds in a date string since midnight of 1-1-1970 then you will have to use
Date.UTC();. However if you are attempting to get properties, in different forms, of the new Date(2010, 11, 17, 0, 0, 0, 0); then you’ll have to use its own constructor methods (toUTCString()orgetUTCMilliseconds()and etc).UTC appears to be a member of the Date constructor itself, and not a member of Date instances. So, in order to invoke UTC you have to use
Date.UTC();Date()converts current time to a string, according to universal time andDate.UTC()retrieves and use value that is calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC). So, they are like a ‘static’ functions.Moreover, in JavaScript whenever you use the
'new'keyword to create a new object (instantiate), thethisvalue of the constructor points to the new Object. So, hello can have a date of its own as oppose toDate()orDate.UTC()‘sthiswould be pointing to a different scope (global i think) which would do its calculation based on 1-1-1970 00:00:00 or return the time which Date function is invoked. The Object hello, on the other hand, would have a base date which was instantiated withnew Date(2010, 11, 17, 0, 0, 0, 0)with its set of constructed methods (toUTCString();and etc). The new Date withthispointing to the new Object using the passed properties as the base “date” value.With all these being said,
hello.UTC()is accessing a function that is not a member of its constructor and thus doesn’t work. This is part of the OOP in JavaScript. This is all on top of my head and probably a bit fuzzy if you are reading this. Please correct me if i have errors.