B4J Question Add 30 seconds to Time

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using the following code to generate a time & date string..

B4X:
Dim current_time As String = DateTime.GetHour(DateTime.Now) & ":" & DateTime.GetMinute(DateTime.Now) & ":" & DateTime.GetSecond(DateTime.Now)
    Dim current_date As String = DateTime.GetDayOfMonth(DateTime.Now) & "-" & DateTime.GetMonth(DateTime.Now) & "-" & DateTime.GetYear(DateTime.Now)

    Dim current_year As String = DateTime.GetYear(DateTime.Now)
    Dim current_month As String = NumberFormat(DateTime.GetMonth(DateTime.Now),2,0)
    Dim current_dayofmonth As String = NumberFormat(DateTime.GetDayOfMonth(DateTime.Now),2,0)
    Dim current_hh As String = NumberFormat(DateTime.GetHour(DateTime.Now),2,0)
    Dim current_min As String = NumberFormat(DateTime.GetMinute(DateTime.Now),2,0)
    Dim current_sec As String = NumberFormat(DateTime.GetSecond(DateTime.Now),2,0)

    current_time = current_hh & ":" & current_min & ":" & current_sec
    current_date = current_dayofmonth & "-" & current_month & "-" & current_year
   
    Log(current_time & " " & current_date)

The question I have is how can I add 30 seconds to the following code ?

So if it logs 17:44:34 29-05-2016 how can I make it so it adds 30 seconds to it so it becomes: 17:45:04 29-05-2016

I know this sounds easy but can't work it out.
 

fixit30

Active Member
Licensed User
Longtime User
You should use jDateUtils

The above can then be achieved as follows:

B4X:
   DateTime.DateFormat = "HH:mm:ss dd-MM-yyyy"
Dim current_date As Long  = DateTime.Now
Log(DateTime.Date(current_date))
Dim p As Period
p.Seconds = 30
Log(DateTime.Date(DateUtils.AddPeriod(current_date, p)))
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
B4X:
Dim date_str As String
Dim time_str As String

DateTime.DateFormat = "dd/MM/yyyy"
DateTime.TimeFormat = "HH:mm:ss"

Dim t1 As Long = DateTime.Now

date_str = DateTime.Date(t1)
time_str = DateTime.Time(t1)
Log(date_str & " - " & time_str)

'###################################

Dim t2 As Long = t1 + 30000 'just add 30 sec * 1000 ms to t1 (long)

date_str = DateTime.Date(t2)
time_str = DateTime.Time(t2)
Log(date_str & " - " & time_str)
 
Upvote 0
Top