Android Question Time zone - Strip time off date

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All

My original aim was to get the difference between todays date and a specified date, for "todays" date I only wanted the date, zero time.

In an old thread [2012] Erel posted the following code:

Dim date, time AsLong
DateTime.SetTimeZone(0)
date = DateTime.Now
time = date ModDateTime.TicksPerDay
date = date - time​

This appears to work quite well but I am unsure of the knock-on effects of changing the time zone.
My query is, does setting the time zone(0) have any effects that require the time zone to be "reset" to the local time zone?
I am probably concerned unnecessarily.

Regards Roger
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should use DateUtils.PeriodBetween to calculate difference between dates. All other code is wrong.

You don't need to change the time zone though it will not cause any problems to change it. It only affects the current process.

You can use this code to strip the time:
B4X:
Dim today As Long = DateTime.DateParse(DateTime.Date(DateTime.Now))
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User


Thanks Erel,
I tried using PeriodBetweenInDays as in the Tutorial and only produced errors. If "all other code is wrong" I will go back and try again.

Regards Roger
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Erel,

A further attempt at using DateUtils.PeriodBetweenInDays and it produces a 1 day error. It is 8:20 PM 27 Aug 2014, for an end date of 29 Aug 2014 the code gives a difference of 1 day instead of 2 days. Datetime.now gives the correct date of 27th.
Hopefully you can see what I'm doing wrong, I have hit a brick wall.

Regards Roger

Sub DiffDate_click
DateTime.SetTimeZone(0)
DateTime.DateFormat = "ddMMyy"

Dim Today1 As Long
Dim Enddatestrg As String = "290814" ' Input end date of 29 Aug 2014
Dim Enddate As Long
Dim TimeDiff As Long
Dim Today1strg As String
Dim p As Period

Today1 = DateTime.now
Today1strg = DateTime.Date(Today1)
Enddate = DateTime.DateParse(Enddatestrg)

p = DateUtils.PeriodBetweenInDays(Today1, Enddate)
TimeDiff = p.Days
Msgbox(TimeDiff,"")
End Sub​
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
B4X:
Dim OrigDateFormat As String
OrigDateFormat=DateTime.Dateformat  'store original date format
DateTime.DateFormat="ddMMyy"
Dim EndDate As String ="290814"
Dim TodayDate As String =DateTime.Date(DateTime.Now)
Dim TimeDiff As Long
Dim p As Period
p = DateUtils.PeriodBetweenInDays(DateTime.DateParse(TodayDate),DateTime.DateParse(EndDate))
TimeDiff = p.Days
Msgbox(TimeDiff,"")  'yields 2
DateTime.Dateformat=OrigDateFormat  'Revert back to original date format
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
This certainly works. I can't really see what I was doing wrong but what works, works.
In your following line:
B4X:
p = DateUtils.PeriodBetweenInDays(Today1, Enddate)
You included the time component in today's date. That is why you came up with 1 instead of 2. if you look at my below line, I strip the time component:
B4X:
p = DateUtils.PeriodBetweenInDays(DateTime.DateParse(TodayDate),DateTime.DateParse(EndDate))
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User

A Homer Simpson moment, Doh!

Thanks again
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…