Last active
September 27, 2015 05:48
-
-
Save arshaw/1221373 to your computer and use it in GitHub Desktop.
XDate: Extending the Parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can extend the parser by adding a new parsing function to the `XDate.parsers` array. | |
// This function is given a single string argument and should return an XDate if parsing was successful. | |
function parseMDY(str) { | |
// this example parses dates like "month/date/year" | |
var parts = str.split('/'); | |
if (parts.length == 3) { | |
return new XDate( | |
parseInt(parts[2]), // year | |
parseInt(parts[0] ? parts[0]-1 : 0), // month | |
parseInt(parts[1]) // date | |
); | |
} | |
} | |
XDate.parsers.push(parseMDY); | |
var d = new XDate("6/8/1986"); |
Here's a paser for bootstrap datetimepicker plugins :
function parseDatePicker(str) {
// parses dates like "dd/mm/yyyy hh:mm" or "d/m/yyyy hh:mm"
var parts = str.split(' ');
var dateparts = parts[0].split('/');
var timeparts = parts[1].split(':');
if ((dateparts.length == 3) && (timeparts.length == 2))
return new XDate(
parseInt(dateparts[2] , 10), // year
parseInt(dateparts[1] ? dateparts[1] - 1 : 0 , 10 ), // month
parseInt(dateparts[0], 10), // day
parseInt(timeparts[0] , 10), // hours
parseInt(timeparts[1], 10), // minutes
0 // seconds
);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@morrisdj you can also be sure they are numbers and with exact format with regexp: