6

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?

Brett Zamir
  • 14,034
  • 6
  • 54
  • 77
mpeters
  • 4,737
  • 3
  • 27
  • 41

3 Answers3

5

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.

Jouni K. Seppänen
  • 43,139
  • 5
  • 71
  • 100
mpeters
  • 4,737
  • 3
  • 27
  • 41
  • 6
    Hello, would it be possible to expand out this answer so it's still helpful to future visitors even if the link goes bad? – josliber Nov 29 '15 at 02:05
  • 2
    @josliber was right, the link did go bad. Here's the archived version: . TLDR: write a parser yourself by iterating over the format string symbols and applying `RegExp`s as needed. – Nikita Karamov Jul 09 '22 at 20:51
1

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()...

Brett Zamir
  • 14,034
  • 6
  • 54
  • 77
-4

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);   
Kelly Anderson
  • 366
  • 1
  • 3
  • 1
    I don't think you understand what strptime does. You give it a date string and the format of that date. Consider these 2 formats that I need to use: `%m/%d/%Y` and `%d.%m.%Y`. How would your parser be able to tell that "10/5/2008" is "Oct 5th, 2008" but "10.5.2008" is "April 10th, 2008"? – mpeters Jan 29 '09 at 15:28
  • I had assumed by your description that the only formatting concern that you had was that the date string would be formatted the was people in different regions format their dates. The JavaScript date object knows the region of the browser and will automatically parse dates accordingly. – Kelly Anderson Jan 29 '09 at 20:29
  • 1
    @Kelly Anderson: No, it's not that I want the browser to format the dates based on it's region (which can change for travelers and won't be right for non-native speakers) but it should be formatted based on the user's already chosen language preference. So I need strptime and a formatting string. – mpeters Feb 03 '09 at 14:29