跨时区的日期验证
这听起来可能微不足道。这就是我想要的。
我有一个名为“评估日期”的数据库字段,该字段不应大于今天的日期。我在 html 页面中执行验证,然后将日期作为 JSON 字符串发送到 ASP.Net 应用程序进行保存。该服务器应用程序可能驻留在不同的时区。
显然我必须在服务器上再次执行日期验证。如果用户指定日本的日期为 2009 年 6 月 20 日。HTML 验证将通过。然而,当它到达日期仍然是 2009 年 6 月 19 日的美国服务器时,它将失败。
出生日期的类似验证。它应该验证该日期至少已年满 18 岁。但是当在网络上显示时,无论用户在哪里看到它,它都应该始终显示相同的日期。出生日期在日本可能是18岁但服务器上没有。它仍然应该存储,但是当它显示时,应该显示正确的日期。
这不是日期时间的问题,因为在这种情况下我将使用 UTC 时间。
关于如何验证、保存和显示正确日期有什么想法吗?
谢谢
This may sound trivial. Here's what I want.
I'm had a database field called Date of Assessment, which should not be greater than today's date. I'm performing the validation in the html page and then sending the date as a JSON string to a ASP.Net application to save. This server application may reside in a different timezone.
I obviously have to perform the date validation again on the server. If a user is specifying a date in say Japan as 20th June 2009. The HTML validation will pass. However when it reaches the server in the US where the date is still 19th June 2009, it will fail.
The similar validation for date of birth. It should validate that the date is at least 18 years old. But when displaying on the web, it should always show the same date regardless of where the user is seeing it. The date of birth may be 18 years old in Japan but not in the server. It should still store, but when it displays the correct date should be displayed.
This is not a problem with datetime cos I will be using UTC times in this case.
Any ideas on how to validate, save and display the correct date?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 getTimezoneOffset() 首先获取客户端中的时区,并将该值作为参数发送回来。然后,您的服务器端可以使用该时区偏移进行计算。
You can use getTimezoneOffset() to first get the timezone in client, and send the value back as a param. Your server side can then calculate with this timezone offset.
您可以在后端保存时间戳并根据前端的本地时区验证您的日期。这在您处理不同时区的情况下效果很好。
You can save timestamp at backend and validate your date against local timezone at frontend. This works well in scenario where you deals with various timezones.