getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
问题描述
我正在尝试仅从日期中提取年份,但由于某种原因,它在一年的第一天返回了前一年.
I'm trying to pull just the year out of a date, but for some reason on the first day of the year its returning the year previous.
new Date('2012-01-01').getFullYear()
将返回2011"并且
new Date('2012-01-02').getFullYear()
将返回2012"
关于我做错了什么有什么好的想法吗?或对此进行修复会有所帮助.
Any good ideas on what I'm doing wrong? or a fix for this would be helpful.
推荐答案
new Date('2012-01-01') 将解析日期,假设它是 UTC.创建的 Date 对象包含您的时区,因此在打印 getYear() 的结果时会考虑到这一点.如果你在 GMT-anything,那意味着你要回到前一年.您可以通过调用 Date.prototype.getUTCFullYear() 忽略时区并仅处理 UTC.
new Date('2012-01-01') will parse the date assuming it's in UTC. The created Date object incorporates your timezone, so it will take that into account when printing out the result of getYear(). If you're in GMT-anything, that means you're going back to the previous year. You can ignore timezones and deal with just UTC by calling Date.prototype.getUTCFullYear().
这篇关于getFullYear 在一年的第一天返回前一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:getFullYear 在一年的第一天返回前一年
- 如何显示带有换行符的文本标签? 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
