date对象获取年的问题

getTime() 返回从 1970 年 1 月 1 日至今的毫秒数,那么我用这个获取年为什么是+1900而不是+1970呢?var year=d.getYear()+1900;

阅读 2.4k
2 个回答

请用 getFullYear()

getyear() 被废弃 参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date/getYear

Return Value:
For years greater than or equal to 2000, the value returned by getYear is 100 or greater. For example, if the year is 2026, getYear returns 126.
For years between and including 1900 and 1999, the value returned by getYear is between 0 and 99. For example, if the year is 1976, getYear returns 76.
For years less than 1900, the value returned by getYear is less than 0. For example, if the year is 1800, getYear returns -100.
date.getFullYear()
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题