Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Tue Sep 26, 2017 2:11 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Creating a Date object from a String
PostPosted: Mon May 13, 2013 6:39 am 
Offline

Joined: Fri Nov 16, 2012 11:58 pm
Posts: 6
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)


Top
 Profile  
Reply with quote  
 Post subject: Re: Creating a Date object from a String
PostPosted: Mon May 13, 2013 8:24 am 
Offline
User avatar

Joined: Mon Feb 05, 2007 5:21 pm
Posts: 600
Location: New York, NY
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

_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.


Top
 Profile  
Reply with quote  
 Post subject: Re: Creating a Date object from a String
PostPosted: Tue May 14, 2013 8:49 am 
Offline

Joined: Fri Nov 16, 2012 11:58 pm
Posts: 6
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Creating a Date object from a String
PostPosted: Tue May 14, 2013 1:57 pm 
Offline
User avatar

Joined: Fri Sep 30, 2005 11:48 am
Posts: 3554
Location: Lenexa, KS
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.

_________________
Bob K.

A blog about being a Real Studio/Xojo developer at http://www.bkeeneybriefs.com


Top
 Profile  
Reply with quote  
 Post subject: Re: Creating a Date object from a String
PostPosted: Fri May 17, 2013 8:04 pm 
Offline

Joined: Fri Nov 16, 2012 11:58 pm
Posts: 6
Where can I find more info about SQL Date/Time Format? Thanks.


Top
 Profile  
Reply with quote  
 Post subject: Re: Creating a Date object from a String
PostPosted: Sat May 18, 2013 11:22 am 
Offline
User avatar

Joined: Fri Sep 30, 2005 11:48 am
Posts: 3554
Location: Lenexa, KS
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

_________________
Bob K.

A blog about being a Real Studio/Xojo developer at http://www.bkeeneybriefs.com


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group