Real Software Forums
http://forums.realsoftware.com/

Creating a Date object from a String
http://forums.realsoftware.com/viewtopic.php?f=3&t=47861
Page 1 of 1

Author:  shusseina [ Mon May 13, 2013 6:39 am ]
Post subject:  Creating a Date object from a String

Hi folks

I am reading in data from a file. The data includes date and time values which I initially read from the file as strings.

How do I create a Date object from a String object, where the String holds a date and time? Surely Real Basic must have a built-in method for creating a Date object from such a string?

I have tried parseDate, but it zeros the time component.

// dateTimeString is 23-Nov-10 8:30:00 a.m.
// After call...
// aDate is 2010-11-23 00:00:00
// aValidDate is true
aValidDate = ParseDate(dateTimeString, aDate)

Author:  ktekinay [ Mon May 13, 2013 8:24 am ]
Post subject:  Re: Creating a Date object from a String

I can't recreate your results. When the initial string is "23-Nov-10" or "Nov-23-10", ParseDate fails here.

Assuming your system settings are just different from mine, I tried "11/23/10 8:30:00 a.m." and was able to reproduce your results, but if your file format is not locale-specific, I wouldn't rely on ParseDate anyway. Try something like this:
Function ParseFileDate(text As String, ByRef value As Date) As Boolean
static months() as string = Array( "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" )

dim r as date

dim dateString as string = text.NthField( " ", 1 )
dim timeString as string = text.Mid( dateString.Len + 2 )

dim dateParts() as string = dateString.Split( "-" )
if dateParts.Ubound <> 2 then return false
dim timeParts() as string = timeString.Split( ":" )
if timeParts.Ubound <> 2 then return false

dim day as integer = dateParts( 0 ).Val
dim month as integer = months.IndexOf( dateParts( 1 ) ) + 1
dim year as integer = dateParts( 2 ).Val
if year > 0 and year < 100 then
dim now as new date
dim currentYear as integer = now.Year
dim century as integer = ( currentYear \ 100 ) * 100
year = year + century
end if

dim hour as integer = timeParts( 0 ).Val
dim minutes as integer = timeParts( 1 ).Val
dim seconds as integer = timeParts( 2 ).NthField( " ", 1 ).Val
dim ampm as string = timeParts( 2 ).NthField( " ", 2 )
if ampm = "p.m." then
hour = hour + 12
end if

if day > 0 and month > 0 and year > 0 and hour > 0 then
r = new Date( year, month, day, hour, minutes, seconds )
end if

if not( r is nil ) then
value = r
return true
else
return false
end if

End Function

Author:  shusseina [ Tue May 14, 2013 8:49 am ]
Post subject:  Re: Creating a Date object from a String

Thanks Kem, appreciate that (didn't expect someone to give me the whole shebang).

Still can't believe Real Studio doesn't offer a built-in method to do this.

Author:  Bob Keeney [ Tue May 14, 2013 1:57 pm ]
Post subject:  Re: Creating a Date object from a String

shusseina wrote:
Thanks Kem, appreciate that (didn't expect someone to give me the whole shebang).

Still can't believe Real Studio doesn't offer a built-in method to do this.


Short dates are a location specific detail. What is 11/11/2011 or 11-11-2011? It depends on where you live. So the ParseDate function uses the method local to your region specified in the OS. It's a guessing game. Add in differences between OS's and platforms and you get some fun stuff.

Personally, it's why I really like saving and showing all my dates in SQL Date/Time format. It's consistent and there's no mistaking it.

Author:  shusseina [ Fri May 17, 2013 8:04 pm ]
Post subject:  Re: Creating a Date object from a String

Where can I find more info about SQL Date/Time Format? Thanks.

Author:  Bob Keeney [ Sat May 18, 2013 11:22 am ]
Post subject:  Re: Creating a Date object from a String

shusseina wrote:
Where can I find more info about SQL Date/Time Format? Thanks.


http://docs.realsoftware.com/index.php/Date.SQLDate
http://docs.realsoftware.com/index.php/Date.SQLDateTime

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/