Android Question yesterdays date and time

samikinikar

Member
Licensed User
Longtime User
Can someone please guide how to retrieve yesterdays date and time ?

Date1. Yesterday date and 8:00 am time
Date2. the current date and time

I need to generate a query like "Select * from datemaster where invdate between date1 and date2

B4X:
Dim date2 As Long = DateTime.Now
Dim date1  As Long
Dim p1 As Period
p1.Days = -1
date1= DateUtils.AddPeriod(date2, p1)
Using the above code I am able to get the yesterdays date, but I need to set the yesterdays time as 8.00 am.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub YesterdayAt (Hours As Int) As Long
   Dim p1 As Period
   p1.Days = -1
   Dim yesterday As Long = DateUtils.AddPeriod(DateTime.Now, p1)
   Return DateUtils.SetDateAndTime(DateTime.GetYear(yesterday), _
       DateTime.GetMonth(yesterday), DateTime.GetDayOfMonth(yesterday), Hours, 0, 0)
End Sub

Test:
B4X:
Log($"$DateTime{YesterdayAt(8)}"$)
 
Upvote 0
Top