I'm not in the GMT timezone and new Date('1995-02-22')
gives me
new Date('1995-02-22') <-- Enter this in the console
Tue Feb 21 1995 16:00:00 GMT-0800 (Pacific Standard Time)
I'm coding something related to a birthday, so the year month and day are super important, but the time isn't. What is the proper way to handle this? Using moment.js seems beyond overkill.
Choose one: either create a Date with the correct date in UTC time, or with the correct date in the local timezone. If the timestamp might be used by other clients too, use UTC time for easy compatibility.
If you create the Date with the correct UTC time, then just use the get* UTC methods to retrieve the year/month/day.
const date = new Date('1995-02-22'); console.log( date.getUTCFullYear(), (date.getUTCMonth() + 1), // 0 indexed date.getUTCDate(), );
Or create the Date in local time and use the local versions of the above, like .getFullYear
.
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.