Android Question calculate next day of a datetime string

m643

Member
Licensed User
Longtime User
Hi all,

I try to find out how I can calculate the next day from a datetime string.
I have already a working calculation but that is based on datetime.now, code for that is:
B4X:
DateTime.date(DateTime.Add(DateTime.now,0,0,1))

Now, I have a string with format "2013-08-14 14:00:00" I'm looking for something like this:
B4X:
DateTime.date(DateTime.Add("2013-08-14 14:00:00",0,0,1))

I know that this is not working but I can't find anything how I can parse a datetime and just add 1 day.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are two steps:
1. Parse the date and get the ticks value.
2. Add the required period.

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim s As String = "2013-08-14 14:00:00"
   DateTime.DateFormat = "yyyy-MM-dd"
   DateTime.TimeFormat = "HH:mm:ss"
   Dim i As Int = s.IndexOf(" ")
   Dim ticks As Long = DateTime.DateTimeParse(s.SubString2(0, i), _
     s.SubString2(i + 1, s.Length))
   Dim p As Period
   p.Days = 1
   Dim tomorrow As Long = DateUtils.AddPeriod(ticks, p)
   Log(DateUtils.TicksToString(tomorrow))
End Sub
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Hi m643, something you need to do is very similar to the sample code below..Hope you can find your own way.
DateUtils is very helpful in this case...
Happy coding.

B4X:
Dim YourTime As Long = DateTime.Now
Dim YourNewTime As Long
Dim p1 As Period
p1.Days = 1
YourNewTime = DateUtils.AddPeriod(YourTime, p1)

EDIT: Sorry Erel I just posted few seconds after you, I don't know you already answered...:)
 
Upvote 0

m643

Member
Licensed User
Longtime User
Thanks for all the reply's.

Erel: When I execute your code I get 1376568000000 but I need it in the same format "2013-08-15 14:00:00" (+1)
In my example in combination with Datetime.now I get it in the above format back.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I use the below code. It returns the correct answer, but it is interesting to see what Erel has to say about this:
B4X:
Dim s As String = "2013-08-14 14:00:00"
DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss"
Dim tomorrow As Long=DateTime.Add(DateTime.DateParse(s),0,0,1)
Msgbox(DateTime.Date(tomorrow),"") 'returns 2013-08-15 14:00:00
 
Upvote 0
Top