This project is archived and is in readonly mode.

#111 ✓resolved
danp

Date.defineParser: option to include user defined parser before default parsers

Reported by danp | August 24th, 2009 @ 11:24 AM

while playing with Mootools' Date extension I ran into a problem:

if I want to parse eg. a German date which is '%d.%m.%Y', I can't parse it correctly, because one of the default parsers matches first. So I would appreciate an option for adding parsers before the default parsers.

With other words, instead of pushing the user defined parser to the end of the parser array, include it at the beginning.

I'm no pro at javascript yet, but maybe something like this:

defineParser: function(pattern, as_first){
    if ($type(as_first) == 'boolean' && as_first === true) {
        var parsers = Date.parsePatterns;

        Date.parsePatterns = [pattern.re && pattern.handler ? pattern : build(pattern)];
        Date.defineParsers(parsers);
    }
    else {
        Date.parsePatterns.push(pattern.re && pattern.handler ? pattern : build(pattern));
    }
},

Comments and changes to this ticket

  • Scott Kyle

    Scott Kyle August 24th, 2009 @ 05:50 PM

    • State changed from “new” to “resolved”
    • Assigned user set to “Scott Kyle”

    @danp

    We have a set of localization files for this very purpose. The next release of -more will allow you to more easily include the ones you want into your build, but until then you download the ones you want from GitHub and tack it onto the end of your mootools-more.js file.

    Then at the beginning of your code, you can call:

    MooTools.lang.setLanguage('de-DE');
    

    After that the date parser will correctly parse dates formatted with the day first (%d.%m.%Y).

    I am not convinced that we need to give the option of giving a user-defined parser higher priority than the defaults, but that's something we can at least consider if you don't find the info I just provided as being sufficient.

    To give you a better sense of how that would be accomplished in the JavaScript:

    defineParser: function(pattern, important){
        Date.parsePatterns[important ? 'unshift' : 'push']( pattern.re && pattern.handler ? pattern : build(pattern) );
    }
    

    Thanks for you ticket, please let me know if you have problems including the German language file or if you still feel that giving a user defined parser high priority is a must have. Until then I'm going to mark this ticket as resolved.

  • danp

    danp August 24th, 2009 @ 06:32 PM

    Thank you!
    Did not realized the localization files. f^^
    With them it works perfectly.

    Well, with those localizations I also don't think it is important to give user defined parser a high priority. Anyway, if I come across another situation where it seems to be useful, I'll come back to you.

  • Ferk

    Ferk May 24th, 2011 @ 08:46 PM

    • Milestone order changed from “0” to “0”

    Hi
    I've come against a problem because of the default parsers.

    I'm trying to parse the YouTube time fragment in the URL (ie. the "#t=2m03s" parameter), but by default the Date.parse function is giving me this output.

    > Date.parse("2m03s")
     Thu Jan 01 1970 01:00:00 GMT+0100 (CET)
    > Date.parse("03s")
     Tue May 03 2011 00:00:00 GMT+0200 (CEST)
    

    Is there something I can do?

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The MooTools Extensions

People watching this ticket

Pages