Android Question KVS Cloud Items and time window

luke2012

Well-Known Member
Licensed User
Longtime User
Hi all,
reading the B4X DateUtils Tutorial from @Erel (https://goo.gl/R7NcFC) I'm trying to understand how to modify my code in order to select items created within a period.

Actually I select (query) the KVS Cloud items using this code:

B4X:
'Each item is a type (Order) and have a field (property) named "Date" that is filled on item creation

'Item creation...
Private TodayDateInTicks as long = DateTime.DateParse (DateTime.Date(DateTime.Now))
NewOrder.Date = Starter.TodayDateInTicks
....

'Query items by today date

Private lstOrders As List
lstOrders.Initialize
Private items As Map = GetAll (aUserKey)
For Each o As Object In items.Values
If o Is Order Then
   Private Ord As Order = o
   If Ord.Date = Starter.TodayDateInTicks Then
      lstOrders.Add(Ord)
   End If
End If
Next

With this code I have all created items within the today date.

How to have the today date order plus orders created the previous day within a specific time window (x hours).

With the above code I got all the items created from 15/11/2017 00:00 to 15/11/2017 23:59.
How to get also the items created (Date) between 14/11/2017 XX:00 and 14/11/2017 23:59 ?

The problem is that my user need to see also the items created the day before (x hours near the midnight) when the day switch to the new day (because he must work on this items also in the first hours of the new day).

Thanks in advance for your help.
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
DateTime.DateParse (DateTime.Date(DateTime.Now))
This pretty much strips the time info out. Since you are only working with the date portion of the "ticks" involved, you're not going to be able to retrieve what you want. If you need to do that for the future, you need to just store DateTime.Now for your order date (that now includes the time).
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
Schermata 2017-11-16 alle 10.17.31.png
This pretty much strips the time info out. Since you are only working with the date portion of the "ticks" involved, you're not going to be able to retrieve what you want. If you need to do that for the future, you need to just store DateTime.Now for your order date (that now includes the time).

Within the order I store Date and time in this way:

Note that the online help says that .Now gets the current time...

B4X:
'Starter...
Private TodayDateInTicks as long = DateTime.DateParse (DateTime.Date(DateTime.Now))

'Order creation...
NewOrder.Date = Starter.TodayDateInTicks
NewOrder.Time = DateTime.Now
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Now gets the current time.
Yeah, but that includes the date (it's a date/time combo).

1) For your initial question. Use DateUtils.SetDateAndTime to create your start date/time and end date/time variables. Then use comparison to see if Order.Time falls within that range.

2) Technically you may not need NewOrder.Date. The only difference between this variable and NewOrder.Time is that the time has been stripped out for the NewOrder.Date variable. Using DateUtils you can change
B4X:
   If Ord.Date = Starter.TodayDateInTicks Then
to
B4X:
  If DateUtils.IsSameDay(Ord.Time, Starter.TodayDateInTicks) Then
You could just set Start.TodayDateInTicks to DateTime.Now and the above comparison with DateUtils.IsSameDay will still work.
 
Upvote 0
Top