使用 JS Date 函数时如何避免区域设置时区转换
在javascript中使用new Date()时如何避免本地时区转换,例如,今天是“2022-03-24”,如果我使用new Date(“2022-03-24”),我会得到不同的结果不同国家/地区的日期,如何避免这种对话并仅使用日期,由于日期选择器,我还需要日期对象。
这是一个解决方案吗?
let date = new Date(2022, 02, 24, 0, 0, 0, 0)
How to avoid the local timezone conversions when working with new Date() in javascript, for example, today is "2022-03-24", if I use new Date("2022-03-24") I'll be getting different dates in different countries, how to avoid this conversation and use only the date, also I need the date object because of the date picker.
is this a solution?
let date = new Date(2022, 02, 24, 0, 0, 0, 0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几点:
new Date("2022-03-24")
示例的输入将被解释为午夜 UTC。它与new Date("2022-03-24T00:00:00.000Z")
相同。使用new Date(Date.UTC(2022, 2, 24, 0, 0, 0, 0))
也会得到相同的结果。new Date(2022, 2, 24, 0, 0, 0, 0)
示例会将输入解释为本地时间。它提供与new Date("2022-03-24T00:00:00.000")
相同的输出(不带Z
)。Date
对象本身没有时区 - 它只是基于 UTC 的 Unix 时间戳的包装。Date
对象执行的操作与您是否会观察时区转换和创建时区转换一样重要。例如,toString
将提供以本地时间表示的字符串表示形式,而toISOString
将提供以 UTC 表示的字符串表示形式。toISOString
)或获取/set 以 UTC 表示(例如getUTCHours
、setUTCHours
等)Date
对象的属性,但某些会返回 yyyy-mm-dd 格式的字符串。如果您的目标是将所选日期传递给后端 API,则应避免使用Date
对象。只需取出字符串并发送即可。标准就是一个很好的例子。请参阅文档了解其
值< /code> 属性
了解更多详细信息。
Date
对象,那么它很可能是根据用户的本地时区构建的。您应该自己创建 yyyy-mm-dd 格式的日期字符串(使用基于本地时间的属性getFullYear
、getMonth
和getDate
),然后将其传递给您的 API。A few things:
new Date("2022-03-24")
will have the input will be interpreted as midnight UTC due to the format you used. It's identical tonew Date("2022-03-24T00:00:00.000Z")
. You'd also get the same result withnew Date(Date.UTC(2022, 2, 24, 0, 0, 0, 0))
.new Date(2022, 2, 24, 0, 0, 0, 0)
will have the input interpreted as local time due to the constructor you used. It gives the same output asnew Date("2022-03-24T00:00:00.000")
(without theZ
).Date
object itself has no time zone - it is just a wrapper around a UTC-based Unix timestamp.Date
object has as much to do with whether you will observe a time zone conversion as creating it does. For example,toString
will provide a string representation in terms of local time, buttoISOString
will provide a string representation in terms of UTC.toISOString
) or get/set in terms of UTC (likegetUTCHours
,setUTCHours
, etc.)Date
object, but some will instead give you back a string in yyyy-mm-dd format. If your goal is to pass along the chosen date to a back end API, you should avoid using aDate
object. Just take the string and send that instead. The standard<input type="date">
is a good example. See the docs for itsValue
property for more details.Date
object, then chances are it was constructed in terms of the user's local time zone. You should make a date string in yyyy-mm-dd format yourself (using the local time based propertiesgetFullYear
,getMonth
, andgetDate
), and then pass that to your API.您可以使用 Date.UTC() 创建 UTC 日期。这样,您就可以避免本地时区转换。
You can use Date.UTC() to create a date in UTC. This way, you avoid the local time zone conversion.