Skip to content

Instantly share code, notes, and snippets.

@arshaw
Last active September 27, 2015 05:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arshaw/1221373 to your computer and use it in GitHub Desktop.
Save arshaw/1221373 to your computer and use it in GitHub Desktop.
XDate: Extending the Parser
// 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");
@anatolyg
Copy link

there is a small bug in this code, months are 0 based, so you need to subtract 1 before creating a new date:

function parseMDY(str) {
        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
                ); 
        }
    }

@morrisdj
Copy link

morrisdj commented Sep 2, 2012

Here's a paser for MySQL datetime strings:

function parseMySQL(str) {
    // parses dates like "yyyy-mm-dd hh:mm:ss"
    var parts = str.split(' ');
    var dateparts = parts[0].split('-');
    var timeparts = parts[1].split(':');
    if ((dateparts.length == 3) && (timeparts.length == 3)) 
        return new XDate(
            parseInt(dateparts[0]), // year
            parseInt(dateparts[0] ? dateparts[0]-1 : 0), // month
            parseInt(dateparts[2]), // day
            parseInt(timeparts[0]), // hours
            parseInt(timeparts[1]), // minutes
            parseInt(timeparts[2])  // seconds
        );
}

@huafu
Copy link

huafu commented Aug 20, 2014

@morrisdj you can also be sure they are numbers and with exact format with regexp:

function parseMySQL(str) {
    if ( (m = str.match(/^([0-9]{4})\-([0-9]{2})\-([0-9]{2}) ([0-9]{2})\:([0-9]{2})\:([0-9]{2})$/)) ) {
        m.pop();
        for ( i = 0; i < m.length; i++ ) m[i] = parseInt(m[i], 10);
        m[1]--; // 0 based month
        return new XDate(m[0], m[1], m[2], m[3], m[4], m[5]);
    }
}

@seebosmile
Copy link

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