Determine whether or not a specified string is a valid Date format

I would like to be able to check whether or not the supplied string is a valid Date format. In looking at the DXL help you would think this is the ideal function:

attributeValue

Declaration

bool attributeValue(AttrDef attr, string s, bool bl)

Operation
Tests whether the supplied string represents a valid value for the specified attribute definition. If the third argument is supplied and set to true, the function will return true if the attribute base type is date and the string is a valid date string for the user's current Locale setting.

However it does not work. I would rather avoid having to use noError() and lastError() to trap DXL errors and am hoping there is a way to check for what I am trying to achieve so for example whatever this 'check' (and I am hoping someone knows what I can use for DXL code that would constitute this check) is it would treat the following as valid dates:

2/14/12
02 February 2012
etc...

And treate the following as invalid dates:

Alex
Bob
02,02,02
etc...

Any help would much be appreciated.
DOORSWizard - Tue Feb 14 10:17:46 EST 2012

Re: Determine whether or not a specified string is a valid Date format
llandale - Tue Feb 14 11:12:41 EST 2012

Date    GetDate(string in_string)
{     // If the string is a valid Date then convert it to Date
        //      Otherwise return null
        noError()
        Date dat = in_string
        if (!null lastError()) dat = null
        return(dat)
}
 
void    Test(string in_string)
{     Date dat = GetDate(in_string)
        print (!null dat) "\t[" in_string "]\t[" dat "]\n"
}
 
Test("2/14/12")
Test("2/14/12 00:02:30")
Test("02 February 2012")
 
Test("Alex")
Test("Bob")
Test("02,02,02")


-Louie

Re: Determine whether or not a specified string is a valid Date format
DOORSWizard - Tue Feb 14 11:35:32 EST 2012

llandale - Tue Feb 14 11:12:41 EST 2012

Date    GetDate(string in_string)
{     // If the string is a valid Date then convert it to Date
        //      Otherwise return null
        noError()
        Date dat = in_string
        if (!null lastError()) dat = null
        return(dat)
}
 
void    Test(string in_string)
{     Date dat = GetDate(in_string)
        print (!null dat) "\t[" in_string "]\t[" dat "]\n"
}
 
Test("2/14/12")
Test("2/14/12 00:02:30")
Test("02 February 2012")
 
Test("Alex")
Test("Bob")
Test("02,02,02")


-Louie

Thanks Louie, per my original post however I was wondering if this can be done without resorting to noError and lastError?

Re: Determine whether or not a specified string is a valid Date format
Mathias Mamsch - Tue Feb 14 12:50:09 EST 2012

DOORSWizard - Tue Feb 14 11:35:32 EST 2012
Thanks Louie, per my original post however I was wondering if this can be done without resorting to noError and lastError?

You can explicitly check for certain locales (1033 -> American) and date formats. I allow myself to modify Louies example. Note that you should explicitly decide which formats are valid, since especially DD/MM/YYYY and MM/DD/YYYY cannot be differentiated in all cases. Regards, Mathias
 

Date    GetDate(string in_string)
{     // If the string is a valid Date then convert it to Date
        //      Otherwise return null
        Date dat = date (in_string, locale 1033, "MM/dd/yy")
        if (null dat) dat = date (in_string, locale 1033, "dd MMMM yyyy")
        return(dat)
}
 
void    Test(string in_string)
{     Date dat = GetDate(in_string)
        print (!null dat) "\t[" in_string "]\t[" dat "]\n"
}
 
Test("2/14/12")
Test("2/14/12 00:02:30")
Test("02 February 2012")
 
Test("Alex")
Test("Bob")
Test("02,02,02")

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

 

Re: Determine whether or not a specified string is a valid Date format
llandale - Tue Feb 14 12:54:27 EST 2012

DOORSWizard - Tue Feb 14 11:35:32 EST 2012
Thanks Louie, per my original post however I was wondering if this can be done without resorting to noError and lastError?

Doh! Sometimes I'm rather blind...

void    Test(string in_string)
{     Date dat = date(in_string)
        print (!null dat) "\t[" in_string "]\t[" dat "]\n"
}
 
Test("2/14/12")
Test("2/14/12 00:02:30")
Test("02 February 2012")
 
Test("Alex")
Test("Bob")
Test("02,02,02")


-Louie