Android Question Add Hours to DateTime

DataProtec

Member
Licensed User
I have read all Threads and testet but can't get the result i need
I get a date with Time from MSSQL DB like 2020-03-08 09:00:00 and have to add 08:00 Hours so the Result should be 2020-03-08 17:00:00
If i convert to long all time missing 1 Hour
Whats the correct way to do this
Thanks in advanced
 

JohnC

Expert
Licensed User
Longtime User
B4X:
8HoursFromNow = DateTime.Now + (DateTime.TicksPerHour * 8)
 
Upvote 0

DataProtec

Member
Licensed User
Hi Erel
Your example Works, but i have the value of Period as 08:00 in the DB and can't convert it to long.
Int Will not be posible because the Value of Period can be 08:15 or 08:30
How can i convert this and add to the Time (t)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Use the date plus time. Get a valid LONG for this date and time.
Create a Period and configure it.
Add the Period to the Date long.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
B4X:
DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss"
    Dim t As Long = DateTime.DateParse("2020-03-08 09:00:00")    
    Dim strTime As String ="08:15"
    Dim strArray() As String =Regex.Split(":", strTime)
    Dim p As Period
    p.Hours = strArray(0)
    p.Minutes= strArray(1)
    Dim t2 As Long = DateUtils.AddPeriod(t, p) 
    Log(DateTime.Date(t2))   'displays: 2020-03-08 17:15:00
 
Upvote 0

DataProtec

Member
Licensed User
Hi Erel
Think the example above Works for me, need to do some more tests
What i did
Swift:
sCheckIn = Crsr.GetString2(2)        '** from DB - 2020-03-09 09:30:00.000'
        sHoursToWorkPerDay  = Crsr.GetString2(3)     '** from DB - 08:45'

Dim p As Period
    Dim i As Int = sHoursToWorkPerDay.IndexOf(":")
    p.Hours = sHoursToWorkPerDay.Substring2(0, i)
    Log("p " & p.Hours)
    p.Minutes = sHoursToWorkPerDay.SubString(i + 1)
    Log("p " & p.Minutes)
    Dim t2 As Long = DateUtils.AddPeriod(lCheckIn, p) 'DateUtils library
    Log("Result " & DateTime.Date(t2)) '** gives me Result 2020-03-09 18:15:00'

Hope it's correct what i did, at least gives me the proper Result if i change the Values in the DB
Thanks a lot everbody
 
Upvote 0
Top