JavaScript 中的 strptime
我有一个日期输入字段,允许用户输入日期,并且我需要验证此输入(我已经有服务器端验证),但技巧是格式取决于区域设置。 我已经有一个用于将 strptime 格式字符串转换为用户首选项的系统,并且我想使用相同的格式在 Javascript 端进行验证。
有任何关于 Javascript 中 strptime()
实现的想法或链接吗?
I have a date input field that allows the user to enter in a date and I need to validate this input (I already have server side validation), but the trick is that the format is locale dependent. I already have a system for translating the strptime format string to the the user's preference and I would like to use this same format for validating on the Javascript side.
Any ideas or links to a strptime()
implementation in Javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过几天的谷歌搜索,我发现 这个实现 虽然不完整,但似乎可以处理我现在遇到的所有情况。
After a few days of googling I found this implementation which, although not complete, seems to handle all of the cases I have right now.
我刚刚添加了 strptime() 的 php.js 实现; 我已经对其进行了一些测试,但还需要进一步的单元测试。 无论如何,请随意尝试一下; 它应该涵盖 PHP 所做的一切(除了尚未支持未记录的 %E...(替代区域设置格式)说明符)。
请注意,它还取决于我们对 setlocale() 和 array_map() 的实现...
I've just added our php.js implementation of strptime(); I've tested it a bit, but it needs further unit testing. Anyhow, feel free to give it a shot; it should cover everything that PHP does (except for not yet supporting the undocumented %E... (alternative locale format) specifiers).
Note that it also depends on our implementation of setlocale() and array_map()...
下面是一个示例函数,它复制了 strptime 的大部分功能。 JavaScript 日期对象通常会解析您向其抛出的任何日期字符串,因此您不必对此太担心。 因此,一旦您有了基于字符串的日期对象,您只需将每个元素推入 JS 对象并返回它即可。 该网站对 JavaScript 日期对象的属性有一个很好的参考:http://www.javascriptkit .com/jsref/date.shtml
Here is an example function that duplicates most of the functionality of strptime. The JavaScript date object generally will parse any date string you throw at it so you don't have to worry to much about that. So once you have a date object based off of your string you just push each element into a JS object and return it. This site has a good reference to the properties of the JavaScript date object: http://www.javascriptkit.com/jsref/date.shtml