JavaScript 中的 strptime

发布于 2024-07-12 22:47:11 字数 197 浏览 10 评论 0原文

我有一个日期输入字段,允许用户输入日期,并且我需要验证此输入(我已经有服务器端验证),但技巧是格式取决于区域设置。 我已经有一个用于将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

捶死心动 2024-07-19 22:47:11

经过几天的谷歌搜索,我发现 这个实现 虽然不完整,但似乎可以处理我现在遇到的所有情况。

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.

谁的年少不轻狂 2024-07-19 22:47:11

我刚刚添加了 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()...

葬シ愛 2024-07-19 22:47:11

下面是一个示例函数,它复制了 strptime 的大部分功能。 JavaScript 日期对象通常会解析您向其抛出的任何日期字符串,因此您不必对此太担心。 因此,一旦您有了基于字符串的日期对象,您只需将每个元素推入 JS 对象并返回它即可。 该网站对 JavaScript 日期对象的属性有一个很好的参考:http://www.javascriptkit .com/jsref/date.shtml

        function strptime(dateString){
            var myDate = new Date(dateString);
            return {tm_sec:myDate.getSeconds(), 
                            tm_min: myDate.getMinutes(), 
                            tm_hour: myDate.getHours(), 
                            tm_mday: myDate.getDate(),
                            tm_mon: myDate.getMonth(), 
                            tm_year: myDate.getFullYear().toString().substring(2), 
                            tm_wday: myDate.getDay()};

        }

        var dateString = "October 12, 1988 13:14:00";       
        dateObj = strptime(dateString);
        document.write("d:" + dateObj.tm_min + "/" + dateObj.tm_hour + "/" + dateObj.tm_mday + "/" + dateObj.tm_mon + "/" + dateObj.tm_year);   

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

        function strptime(dateString){
            var myDate = new Date(dateString);
            return {tm_sec:myDate.getSeconds(), 
                            tm_min: myDate.getMinutes(), 
                            tm_hour: myDate.getHours(), 
                            tm_mday: myDate.getDate(),
                            tm_mon: myDate.getMonth(), 
                            tm_year: myDate.getFullYear().toString().substring(2), 
                            tm_wday: myDate.getDay()};

        }

        var dateString = "October 12, 1988 13:14:00";       
        dateObj = strptime(dateString);
        document.write("d:" + dateObj.tm_min + "/" + dateObj.tm_hour + "/" + dateObj.tm_mday + "/" + dateObj.tm_mon + "/" + dateObj.tm_year);   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文