Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String to Date

Status
Not open for further replies.

conio96

Programmer
Joined
Dec 5, 2002
Messages
4
Location
MY
Hi,
I will extract record from text file and update it to database. But I will receive a type mismatch error when trying to update a string to a date field.
So, is there any way to check whether that particular string is a date or not, if it is, then insert to the table, else skip.

Thanks a lot.
 
Create a COleDateTime with the values by spluiiting them
If the object of the coledatetime says date is valid proceed to update.
 
Solution 1:
As preview answer suggests to use COleDateTime

Use
BOOL ParseDateTime( LPCTSTR lpszDate, DWORD dwFlags = 0, LCID lcid = LANG_USER_DEFAULT );
throw( CMemoryException );
throw( COleException );

Example that works:

COleDateTime dt;
BOOL bOk = FALSE;
TRY
{
bOk = dt.ParseDateTime("January 14, 2004 9:31:01");
}
CATCH (CException e)
{
// Capture error here
}
END_CATCH
if (bOk)
{
// The string represents a datetime but it is depending on the LANG_USER_DEFAULT abd using system locale default
//
}
else
{
// Try another parsing by changing dwFlags
else
// The string does not represent a date time
}

Solution 2: No COleDateTime
Use regular expression to match the string with a datetime pattern.

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top