UNIX timestamp from date + time

peacemaker

Expert
Licensed User
Longtime User
HI, All

If i get Date and Time from a user by 2 dialogs - how to get the UNIX timestamp of GMT 0 noting the TimeZoneOffset ? I know that 1000 ticks is a sec :) - i need just a alghorithm.
Seems, Time dialog returns ticks including the Date ?
 
Last edited:

lagore

Active Member
Licensed User
Longtime User
Yes the time dialogue returns a millisecond value using today's date, the easiest thing to do is convert the time to millisecond (h*60 + m) * 60000 then using the date function this gives the time/date for midnight add the two and you have a correct representation of the input time and date, if you want to correct for time zone add in the time zone offset from the datetime function.

Sent from my HTC One X using Tapatalk 2
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
utc = d + t - DateTime.DateParse(DateTime.Date(DateTime.Now)) - (DateTime.TimeZoneOffset * DateTime.TicksPerHour)

is it correct ?
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Almost,
B4X:
Dim input_date As String = "25/12/2012"
   Dim input_hour As Int = 21      'input time 2130
   Dim input_min As Int = 30
   Dim UTC_datetime As Long
   UTC_datetime = DateTime.DateParse(input_date) + (input_hour * DateTime.TicksPerHour) + (input_min * DateTime.TicksPerMinute) + DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(input_date))
I would use GetTimeZoneOffsetAt not TimeZoneOffset as 'TimeZoneOffset' gets the offset for the current date but 'GetTimeZoneOffsetAt' gets the offset for the specified date taking DST (summer/winter time) into account but then I use this for creating calendar events which could be in a different summer/winter time, also the offset is added not subtracted. Play around with it to see how it works out.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Thanks, lagore !
If not to forget to make + ( DateTime.GetTimeZoneOffsetAt(DateTime.DateParse(input_date)) * DateTime.TicksPerHour) - seems, at last it's correct :)

utc_datetime = d + t - DateTime.DateParse(DateTime.Date(DateTime.Now)) + (DateTime.GetTimeZoneOffsetAt(d) * DateTime.TicksPerHour)
where d, t - Date, Time from dialogs.
 
Last edited:
Upvote 0
Top