views:

410

answers:

4

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?

A: 

You could push the format, from the preferences, into javascript and make it available client-side.

<script type="text/javascript">
    var dateFormat = "dd.mm.Y";
</script>
kRON
Sending the format client side isn't the problem. It's doing the actual validation from that format.
mpeters
A: 

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
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
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
@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
+1  A: 

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.

mpeters
A: 

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

// Can't post the dependencies URLs yet (max one URL for new stackoverflow posters)...

  • github.com/kvz/phpjs/blob/master/functions/strings/setlocale.js
  • github.com/kvz/phpjs/blob/master/functions/array/array_map.js
Brett Zamir