IsDate ( )

Function stats

Average user rating
266
330
9999
Support
FileMaker 10.0 +
Date posted
24 April 2012
Version
1
Recursive function
No

Author Info
 Rewolfer

5 functions

Average Rating None

author_avatar



 

Function overview

Prototype

IsDate  ( _var )


Parameters

_var  any value


Description

Tags:  Validate   Date  

FORM:        IsDate ( _var )

PURPOSE:     return valid date as number of days since 0001-01-01 or nothing if invalid

PARAMETER:    _var    type: unknown    value: unknown
        valid date: "2003-1-23" / "2003-01-01" / "2003-1-1" / "23. 1.2003" / " 1/23/2003"
        invalid date: "03-10-01" / "30.02.2003" / "20030123" / 735223 (FMP internal format)

RESULT:        number of days since 0001-01-01 or nothing

DEPENDENCY:    -

NOTE:        to import foreign dates set "auto calculated value" of your date field to "GetAsDate ( IsDate ( Self ))"
        to get date according to your system-/file-settings write GetAsDate ( IsDate ( _dateInQuestion ))
        requires 4-digit-year (otherwise what is 10/10/10)
        whitespace is tolerated
        accepted formats: "Y-M-D" and "D.M.Y" and either "M/D/Y" (default) or "D/M/Y"
        you have to set $$IsDateFormatPreferred to "D/M/Y" via script if you prefer UK-dates instead of US-dates and vice versa (so 1/4/2003 => 2003-04-01)

Examples

Sample input

"11/23/2003"


Sample output

731542

 

Function code

/*=================================================

FORM:        IsDate ( _var )

PURPOSE:     return valid date as number of days since 0001-01-01 or nothing if invalid

PARAMETER:    _var    type: unknown    value: unknown
        valid date: "2003-1-23" / "2003-01-01" / "2003-1-1" / "23. 1.2003" / " 1/23/2003"
        invalid date: "03-10-01" / "30.02.2003" / "20030123" / 735223 (FMP internal format)

RESULT:        number of days since 0001-01-01 or nothing

DEPENDENCY:    -

NOTE:        to import foreign dates set "auto calculated value" of your date field to "GetAsDate ( IsDate ( Self ))"
        to get date according to your system-/file-settings write GetAsDate ( IsDate ( _dateInQuestion ))
        requires 4-digit-year (otherwise what is 10/10/10)
        whitespace is tolerated
        accepted formats: "Y-M-D" and "D.M.Y" and either "M/D/Y" (default) or "D/M/Y"
        you have to set $$IsDateFormatPreferred to "D/M/Y" via script if you prefer UK-dates instead of US-dates and vice versa (so 1/4/2003 => 2003-04-01)

EXAMPLE:    IsDate ( "11/23/2003" ) => 731542

HISTORY:    2012-04-12 [103032]     1.00     tsw     decided to bring the date-check in its own function
        2012-04-16 [213412]     2.00     tsw     checks all date formats formally and returns either date as a number or nothing
        2012-04-17 [104813]     2.10     tsw     checks for VALID date as well before returning anything
        2012-04-19 [204950]     2.20     tsw     at first run caches the current system format in $$IsDateFormatAccepted
        2012-04-21 [141255]     2.21     tsw     switched to matrix as more reliable
        2012-04-22 [130128]     2.22     tsw     optimized and debugged

SOURCE:        tsw @ http://rewolfer.de
=================================================*/

Let (
_clean = Filter ( _var ; "0123456789./- ");

Case (
    _clean = _var ;
    Let ([
    // first run sets $$IsDateFormatAccepted to either "Y-M-D" or "D.M.Y" or "D/M/Y" or "M/D/Y"
    _accepted = Case ( IsEmpty ( $$IsDateFormatAccepted ) ;
        Let ([
            _dateDelimiter = Left( Filter ( Get (CurrentDate) ; "-./" ); 1);
            _dateFormat = Case (
                GetAsDate ( "2000-12-31" ) = "2000-12-31" ; "Y-M-D" ;
                GetAsDate ( "12/31/2000" ) = "12/31/2000" ; "M/D/Y" ;
                GetAsDate ( "31.12.2000" ) = "31.12.2000" ; "D" & _dateDelimiter & "M" & _dateDelimiter & "Y" )
        ];
            Evaluate("Let( $$IsDateFormatAccepted = \"" & _dateFormat & "\" ; \"\" )" )));
    _format = $$IsDateFormatAccepted ;
    _delimiter = Left ( Filter ( _var ; "-./" ); 1);
    _vl = Substitute ( _var ; [ " " ; "" ] ; [ _delimiter ; ¶ ] );
    _vl1 = GetValue ( _vl ; 1 );
    _vl2 = GetValue ( _vl ; 2 );
    _vl3 = GetValue ( _vl ; 3 );
    _matrixed = Case (
        // YMD = 2003-01-25
        _delimiter = "-" and Length ( _vl1 ) = 4 ; Substitute ( _format ; ["Y" ; _vl1] ; ["M" ; _vl2] ; ["D" ; _vl3]);

        // DMY = 25.01.2003
        _delimiter = "." and Length ( _vl3 ) = 4 ; Substitute ( _format ; ["Y" ; _vl3] ; ["M" ; _vl2] ; ["D" ; _vl1]);

        // MDY = 01/25/2003
        _delimiter = "/" and Length ( _vl3 ) = 4 and $$IsDateFormatPreferred <> "D/M/Y"; Substitute ( _format ; ["Y" ; _vl3] ; ["M" ; _vl1] ; ["D" ; _vl2]);

        // DMY = 25/01/2003; set $$IsdateFormatPreferred via script if you prefer UK-dates over US-dates
        _delimiter = "/" and Length ( _vl3 ) = 4 and $$IsDateFormatPreferred <> "M/D/Y"; Substitute ( _format ; ["Y" ; _vl3] ; ["M" ; _vl2] ; ["D" ; _vl1])
    );
    _dateValid = GetAsDate ( _matrixed )
    ];

    Case ( _dateValid <> "?" ; GetAsNumber ( _dateValid ))
    )
)
)

// ===================================
/*

    This function is published on FileMaker Custom Functions
    to check for updates and provide feedback and bug reports
    please visit http://www.fmfunctions.com/fid/330

    Prototype: IsDate( _var )
    Function Author: Rewolfer (http://www.fmfunctions.com/mid/266)
    Last updated: 24 April 2012
    Version: 1

*/
// ===================================

 

 

 

 

 

 

 

Top Tags

Text Parsing  (31)
Date  (26)
List  (26)
Format  (23)
XML  (22)
Sql  (18)
Dev  (18)
Debug  (15)
Interface  (13)
Text  (13)
Layout  (12)
Variables  (12)
Design  (10)
Filter  (10)
Layout Objects  (8)
Timestamp  (7)