New topic: 2 Simple Questions
<http://forums.realsoftware.com/viewtopic.php?t=46903> Page 1 of 1 [ 7 posts ] Previous topic | Next topic Author Message themttakeover Post subject: 2 Simple QuestionsPosted: Mon Feb 11, 2013 11:30 am Joined: Wed Feb 06, 2013 6:53 pm Posts: 5 Hello, I have code that I use in VB but am having trouble in RB. 1) dim d1 as date date = #2/1/2010# I can't seem to see find a way to set a date to the variable. I tried googling and went through a bunch of links. 2) Cstr(Datediff(dateinterval.day, d1, d2)) This here doesn't seem to work at all. Is there a way today math with dates and have the output be days? Thanks for any and all help guys. Top DaveS Post subject: Re: 2 Simple QuestionsPosted: Mon Feb 11, 2013 11:56 am Joined: Sun Aug 05, 2007 10:46 am Posts: 4533 Location: San Diego, CA Check out PARSEDATE function And one way is to (date1.totalseconds-date2.totalseconds)/(24*60*60*60) Reading the LR will help a lot towards understanding that RealStudio and VB6 are quite different _________________ Dave Sisemore MacPro, OSX Lion 10.7.4 RB2012r1 Note : I am not interested in any solutions that involve custom Plug-ins of any kind Top themttakeover Post subject: Re: 2 Simple QuestionsPosted: Mon Feb 11, 2013 12:20 pm Joined: Wed Feb 06, 2013 6:53 pm Posts: 5 DaveS wrote:Check out PARSEDATE function And one way is to (date1.totalseconds-date2.totalseconds)/(24*60*60*60) Reading the LR will help a lot towards understanding that RealStudio and VB6 are quite different Do you have know of any recourses for the parsedate function? I wasn't able to find much. Is it possible to set like d2 = ParseDate("%yy/%mm/%dd", "09/06/20") ? If not then how do I then call on it later? Top charonn0 Post subject: Re: 2 Simple QuestionsPosted: Mon Feb 11, 2013 12:49 pm Joined: Mon Apr 02, 2007 2:08 am Posts: 1116 Location: San Francisco, CA, USA Dates are objects in RB of class Date. When you instantiate a New date object, it's automatically filled with the data and time that it was created: Dim d1 As New Date 'd2 now contains the current date and time Dim d2 As Date 'd2 is Nil If Not ParseDate("2/1/2010", d2) Then 'Tries to parse the string into d2 'parse error d2 = New Date(d1) 'copy d1 into d2 End If Dim timeDiff As Integer = d1.TotalSeconds - d2.TotalSeconds _________________ Boredom Software Top themttakeover Post subject: Re: 2 Simple QuestionsPosted: Mon Feb 11, 2013 2:02 pm Joined: Wed Feb 06, 2013 6:53 pm Posts: 5 charonn0 wrote:Dates are objects in RB of class Date. When you instantiate a New date object, it's automatically filled with the data and time that it was created: Dim d1 As New Date 'd2 now contains the current date and time Dim d2 As Date 'd2 is Nil If Not ParseDate("2/1/2010", d2) Then 'Tries to parse the string into d2 'parse error d2 = New Date(d1) 'copy d1 into d2 End If Dim timeDiff As Integer = d1.TotalSeconds - d2.TotalSeconds I am slightly confused...Why is d1 being copied into d2? Top lenpartico Post subject: Re: 2 Simple QuestionsPosted: Mon Feb 11, 2013 2:28 pm Joined: Fri Sep 30, 2005 10:49 pm Posts: 491 How about... Dim d1, d2 as date Dim somedatestring, someotherdatestring as string somedatestring = "May 1, 2012" someotherdatestring = "Jan 6, 2013" If not ParseDate(somedatestring, d1) then msgbox "Incorrect date format, please chek date entered" Return end if If not ParseDate(someotherdatestring, d2) then msgbox "Incorrect date format, please chek date entered" Return end if Dim Interval as integer //in seconds Interval = d2.TotalSeconds - d1.TotalSeconds Msgbox "It is now, (" + someotherdatestring + "), " + Str(Interval) + " seconds since " + somedatestring + "." Top charonn0 Post subject: Re: 2 Simple QuestionsPosted: Mon Feb 11, 2013 2:33 pm Joined: Mon Apr 02, 2007 2:08 am Posts: 1116 Location: San Francisco, CA, USA themttakeover wrote:charonn0 wrote:Dim d1 As New Date 'd2 now contains the current date and time Dim d2 As Date 'd2 is Nil If Not ParseDate("2/1/2010", d2) Then 'Tries to parse the string into d2 'parse error d2 = New Date(d1) 'copy d1 into d2 End If Dim timeDiff As Integer = d1.TotalSeconds - d2.TotalSeconds I am slightly confused...Why is d1 being copied into d2? If the ParseDate method fails, it returns False and d2 is still Nil. Trying to access an object that is Nil will raise an error, so d1 is copied to d2 using one of the Date class constructor methods (Constructors are called by using the New keyword.) Passing an existing Date object to the Date constructor will copy the passed date. See: http://docs.realsoftware.com/index.php/Date.Constructor(CopyDate as Date). The above code snippet first tries to parse the string as a Date (If Not ParseDate....) If parsing failed then it uses the Date.Constructor method (New Date(d1)...) to copy d1 to d2. This way, when the last line comparing d1 and d2 is reached, d2 won't be Nil. _________________ Boredom Software Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 7 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
