Page Blocks

Hosting sponsored by:

Point In Space

 

API: Date

Filename:
fwpDate_isValid.ctag

Released With:
5.2.0

Current Version:
1.0

Status: Active

Min Lasso Tested: 8.1.0

Max Lasso Tested: 8.5.3

Related:

Still don't get it?

Check out the talk list archives, or join and ask your questions.

Documentation Error or Request?

Email documentation corrections or comments

API Reference

fwpDate_isValid (Tag)

Description

Accepts a broad variety of string input formats and determines if the input represents a valid date. As of this version it relies on Lasso's valid_date tag (which as we all know, doesn't really tell you if the value is truly valid), but the added value is the ability to specify formats in numerics and text, and get a date string returned in either the default mm/dd/yyyy format or in yyyy-mm-dd format.

Syntax

fwpDate_isValid: dateString;

Parameters & Member Tags

dateString (unnamed) = required : any valid Lasso date or date string

-usaDate = optional : interpret nn/nn/yyyy inputs specifically as a USA date format of mm/dd/yyyy

-euroDate = optional : interpret nn/nn/yyyy inputs specifically as a European date format of dd/mm/yyyy

If neither of the above is provided, the tag will allow for either format. Of course, something like 05/06/2000 will be ambiguous, and will default to USA format, but 13/06/2000 will automatically be determined to be June 13.

-returnAsQ = optional : return the date string back in yyyy-mm-dd format

Examples

fwpDate_isValid: '02/05/2000'; // returns 02/05/2000

fwpDate_isValid: '99/05/2000'; // returns false

fwpDate_isValid: '02/05/2000', -euroDate; // returns 05/02/2000

fwpDate_isValid: '02/05/2000', -euroDate, -formatAsQ; // returns 2000-05-02

fwpDate_isValid: '02/05/2000', -formatAsQ; // returns 2000-02-05

fwpDate_isValid: '15/05/2000', -formatAsQ; // returns 2000-05-15

fwpDate_isValid: 'apr 5 2000'; // returns 04/05/2000

fwpDate_isValid: 'apr 5 2000', -euroDate; // returns 05/04/2000

fwpDate_isValid: '10 June 2000'; // returns 06/10/2000

Source Code

View in separate window

<?lassoscript
//............................................................................
//
//    pageblocks: (c) 2002-2007 http://www.pageblocks.org/
//
//............................................................................
/*

    {fileName=        fwpDate_isValid.ctag }
    {rsrcType=        tag }
    {rsrcName=        fwpDate_isValid }
    {rsrcHTTP=        www.pageblocks.org/refc/fwpDate_isValid }

    {lassoVrsnMin=    8.1.0 }
    {lassoVrsnMax=    8.5.3 }

    {author=        Greg Wilits }
    {authorEmail=    subscribe to pbTalk at www.pageblocks.org/talk/ }
    {authorHTTP=    www.pageblocks.org }

    {desc=            validates that a string is a valid date }

    {maintvsrn=        1.0 }
    {maintrelease=    5.1.7 }
    {maintdate=        2007-05-30 }
    {maintauthor=    Greg Willits }
    {maintnotes=    initial release }

*/
//............................................................................

define_tag:'fwpDate_isValid',
    -required = 'inputDate', -copy;

    local:
        'usaDate'         = false,
        'euroDate'         = false,
        'lassoDate'     = string,
        'returnAsQ'     = false,
        'dateParts'        = array,
        'result'        = true,
        'Qformatted'    = string,
        'thisMonth'        = pair,
        'monthNames'     = (map:
                            '01' = ('jan, january'),
                            '02' = ('feb, february'),
                            '03' = ('mar, march'),
                            '04' = ('apr, april'),
                            '05' = ('may'),
                            '06' = ('jun, june'),
                            '07' = ('jul, july'),
                            '08' = ('aug, august'),
                            '09' = ('sep, september'),
                            '10' = ('oct, october'),
                            '11' = ('nov, november'),
                            '12' = ('dec, december'));
        
    (params >> '-returnAsQ')
        ? #returnAsQ = true;
        
    (params >> '-euroDate')
        ? #euroDate = true;
        
    (params >> '-usaDate')
        ? #usaDate = true;
        
    (#inputDate == '') || (#inputDate >> '0000')
        ? return: false;

//    normalize date input format for internal use

    #inputDate->(replace:'.','/');
    #inputDate->(replace:'-','/');
    #inputDate->(replace:'_','/');
    #inputDate->(replace:' ','/');    

    #dateParts = #inputDate->split:'/';

    if: #dateParts->size == 3;
        local:
            'datePartA' = #dateParts->get:1,
            'datePartB' = #dateParts->get:2,
            'datePartC' = #dateParts->get:3;

        if: #datePartA != string:(integer:#datePartA);
            iterate: #monthNames, #thisMonth;
                if: (#thisMonth->second) >> #datePartA; 
                    #datePartA = (#thisMonth->first);
                    #inputDate = #datePartA + '/' + #datePartB + '/' + #datePartC;
                    #usaDate = true;
                    #euroDate = false;
                    loop_abort;
                /if;
            /iterate;
        /if;
    
        if: #datePartB != string:(integer:#datePartB);
            iterate: #monthNames, #thisMonth;
                if: (#thisMonth->second) >> #datePartB; 
                    #datePartB = (#thisMonth->first);
                    #inputDate = #datePartA + '/' + #datePartB + '/' + #datePartC;
                    #usaDate = false;
                    #euroDate = true;
                    loop_abort;
                /if;
            /iterate;
        /if;

        if: (#datePartA->size == 4);
            #inputDate    = #datePartA + '-' + #datePartB + '-' + #datePartC;
            #lassoDate = #datePartA + '-' + #datePartB + '-' + #datePartC;
        else;
            (#datePartA > 12) || #euroDate
                ? #lassoDate = (#datePartB + '/' + #datePartA + '/' + #datePartC)
                | #lassoDate = (#datePartA + '/' + #datePartB + '/' + #datePartC);
        /if;
        
    /if;

//    validate the date value

    if: #usaDate && !(valid_date:#inputDate, -format='%m/%d/%Y') && !(valid_date:#inputDate);
        #result = false;
        #Qformatted = false;
    else: #euroDate && !(valid_date:#inputDate, -format='%d/%m/%Y') && !(valid_date:#inputDate);
        #result = false;
        #Qformatted = false;
    else: !(valid_date:#inputDate, -format='%m/%d/%Y') && !(valid_date:#inputDate, -format='%d/%m/%Y') && !(valid_date:#inputDate);
        #result = false;
        #Qformatted = false;
    /if;

    #result && ((date:#lassoDate) != null) ? #Qformatted = (date_format: #lassoDate, -format='%Q');
    
    #returnAsQ
        ? return: #Qformatted
        | return: #result;

/define_tag;
?>


© 2002-2012, pageblocks.org