Android Question Getting the time in ticks for tomorrow's date

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

This toast statement will display the time in ticks for today at 2 in the morning.

B4X:
ToastMessageShow(DateTime.DateTimeParse(DateTime.Date(DateTime.Now),"02:00"),True)

How do I do the same thing for 2 in the morning for tomorrow morning?

Thanks.
 

inakigarm

Well-Known Member
Licensed User
Longtime User
From https://www.b4x.com/android/forum/threads/b4x-dateutils-simplifies-date-and-time-calcuations.26290/
'Adds a Period to the given date instance. Do not forget to assign the result.
B4X:
Sub AddPeriod(Ticks As Long, Per AsPeriod) As Long

In this case,Ticks in AddPeriod will be Datetime Ticks at 2:00am and all fields of Type Period will be 0, except Days=1
B4X:
Type Period (Years As Int, Months As Int, _
      Days As Int, Hours As Int, Minutes As Int, Seconds As Int)

I think you can do it also with Smart literal
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
From https://www.b4x.com/android/forum/threads/b4x-dateutils-simplifies-date-and-time-calcuations.26290/
'Adds a Period to the given date instance. Do not forget to assign the result.
B4X:
Sub AddPeriod(Ticks As Long, Per AsPeriod) As Long

In this case,Ticks in AddPeriod will be Datetime Ticks at 2:00am and all fields of Type Period will be 0, except Days=1
B4X:
Type Period (Years As Int, Months As Int, _
      Days As Int, Hours As Int, Minutes As Int, Seconds As Int)

I think you can do it also with Smart literal


Hi,

It's telling me to declare the type in Sub Globals so I'm going to take a look at the sample that comes with DateUtils.

Thanks.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Make sure to check the lib dateutils. The named type comes with this lib
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I tried it but the toast message is crashing the app. Can you help correct my coding?

B4X:
Dim thePeriod As Period

thePeriod.Days = 1
lngTheTimeNow = DateTime.Now

ToastMessageShow(DateTime.DateTimeParse( DateUtils.AddPeriod(DateTime.Date(lngTheTimeNow), thePeriod),"02:00"),True)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Why can't you use something like this:
B4X:
Log(DateTime.DateTimeParse(DateTime.Date(DateTime.Now),"02:00:00") )  
    Log(DateTime.Add(DateTime.DateTimeParse(DateTime.Date(DateTime.Now),"02:00:00"),0,0,1) )  'add 1 day
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Sussed it out.

B4X:
lngTheTimeNow = DateTime.Now
intHours = DateTime.GetHour(lngTheTimeNow)
intMinutes = DateTime.GetMinute(lngTheTimeNow)
intSeconds = DateTime.GetSecond(lngTheTimeNow)
intYear = DateTime.GetYear(lngTheTimeNow)
intMonth = DateTime.GetMonth(lngTheTimeNow)
intDay = DateTime.GetDayOfMonth(lngTheTimeNow)

ToastMessageShow(DateUtils.SetDateAndTime(intYear,intMonth,intDay + 1,2,0,0),True)
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Why can't you use something like this:
B4X:
Log(DateTime.DateTimeParse(DateTime.Date(DateTime.Now),"02:00:00") ) 
    Log(DateTime.Add(DateTime.DateTimeParse(DateTime.Date(DateTime.Now),"02:00:00"),0,0,1) )  'add 1 day
Thanks everyone for the replies. We are going for this coding since it uses less coding.
 
Upvote 0
Top