This article quickly answers the problem of years display errors that new JavaScript may encounter when declaring dates. You may have encountered a situation like "JavaScript date returns to future years, I'm seeing 2012 instead of 2011, what's going on?" Please see the following example: The target date is Friday, July 15, 2011.
var todaysDate = new Date(); console.log(todaysDate); //输出:Date {Fri Aug 12 2011 18:45:53 GMT+1000} var expiryDate = new Date('15/07/2011'); console.log(expiryDate); //输出:Date {Wed Mar 07 2012 00:00:00 GMT+1000} var expiryDate = new Date('07/15/2011'); console.log(expiryDate); //输出:Date {Fri Jul 15 2011 00:00:00 GMT+1000}
What's the problem? JavaScript's getMonth()
function counts from 0 (0 represents January, 1 represents February, and so on), but that's not the only reason for the year error! If we try to parse the date with a single parameter, we won't get the correct result.
var expiryDate = new Date(2011, 15, 07); console.log(expiryDate); //输出:Date {Sat Apr 07 2012 00:00:00 GMT+1000}
But if we parse in text date format, we can get the correct result!
var expiryDate = new Date('July 15, 2011'); console.log(expiryDate); //输出:Date {Fri Jul 15 2011 00:00:00 GMT+1000}
Date processing is sometimes tricky, so if you need to process dates in large quantities, it is recommended to use the JavaScript date library to simplify operations and improve efficiency! A very comprehensive link to date resources is also provided here. (Link omitted)
FAQs about JavaScript date functions (FAQs)
getYear()
method? The getYear()
method in JavaScript returns the year of the date, which is a double digit, and is the result of subtracting 1900 from the actual year. For example, if the year is 2022, getYear()
will return 122. This can lead to obfuscation and data representation errors. To avoid this, it is recommended to use the getFullYear()
method, which returns the full four-digit year.
getYear()
and getFullYear()
in JavaScript? getYear()
and getFullYear()
is the return format of the year. getYear()
Returns the result of year minus 1900, for years after 2000, the result is a double digit. On the other hand, getFullYear()
returns the full four-digit year, making it more accurate and less likely to be confused.
getYear()
method? To correct the year returned by the getYear()
method, add 1900 to the result. However, it is generally recommended to use the getFullYear()
method instead, as it returns the correct four-digit year directly.
getYear()
method? getYear()
method is related to the Y2K problem. However, this design can cause confusion and errors, so the getFullYear()
method is generally recommended.
getYear()
method to get the current year? Although technically you can get the current year by adding 1900 to the result of the getYear()
method, it is usually recommended to use the getFullYear()
method because it returns the correct four-digit year directly.
You can get the current year by creating a new Date
object and calling the getFullYear()
method on it. For example: let currentYear = new Date().getFullYear();
getYear()
and getFullYear()
methods? getYear()
and getFullYear()
methods return a number representing the year. getYear()
returns the result of year minus 1900, while getFullYear()
returns the full four-digit year.
getYear()
and getFullYear()
methods based on zero like JavaScript arrays? No, getYear()
and getFullYear()
methods are not based on zero. They return the actual year, although getYear()
will subtract 1900 from it.
getYear()
and getFullYear()
methods on future or past dates? Yes, you can use Date
and getYear()
methods for any valid getFullYear()
object, regardless of whether the date is in the future or in the past.
getYear()
and getFullYear()
methods? getYear()
and getFullYear()
methods are well supported in all modern browsers. However, since getYear()
can cause confusion and errors, it is generally recommended to use getFullYear()
.
The above is the detailed content of JavaScript Showing Wrong Year For Date. For more information, please follow other related articles on the PHP Chinese website!