Android Question datetime

davepamn

Active Member
Licensed User
Longtime User
One of my devices is giving the wrong date time. It is showing an hour ahead. How can I tell the date time variable to use the system local time.

B4X:
Sub QryDate As String

   Try

        If TD.IsInitialized=False Then

            TD.Initialize

        EndIf

        Dim Year As String=TD.Year

        Dim Month As String=TD.Month

        Dim Day As String=TD.Day

Catch

    oError.ShowLastException("Error QryDate")

EndTry

        Return Year&Month&Day

End Sub

///Code for TD Initiatize

Public Sub Initialize

        DateTime.DateFormat="MM/dd/yyyy"

        PostDate=DateTime.Date(DateTime.Now)

        Year =NumberFormat2(DateTime.GetYear(DateTime.DateParse(PostDate)),4,0,0,False)

        Month =NumberFormat2(DateTime.GetMonth(DateTime.DateParse(PostDate)),2,0,0,False)

        Day =NumberFormat2(DateTime.GetDayOfMonth(DateTime.DateParse(PostDate)),2,0,0,False)

End Sub
 
Last edited:

davepamn

Active Member
Licensed User
Longtime User
Please use [code] [/code] tags when posting code.

What is the output of: Log(DateTime.Time(DateTime.Now))
And: Log(DateTime.TimeZoneOffset)


Should I use Universal Time?


B4X:
       Dim oOffset As TimeSpan = GetTimeZoneOffset()

        dTime = DateTime.Now().ToUniversalTime().AddHours(oOffset.Hours).ToUniversalTime()

   Function GetTimeZoneOffset() As TimeSpan
        Dim oOffset As TimeSpan
        Dim mst As DateTime = FormatDateTime(Now, 2)
        Dim oLocalTime As TimeZoneInfo
        oLocalTime = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.StandardName)

        oOffset = oLocalTime.GetUtcOffset(mst)

        Return (oOffset)

    EndFunction
 
Upvote 0

pjetson

Member
Licensed User
Longtime User
I found that I had to put this into Activity_Create:

B4X:
DateTime.DateFormat = DateTime.DeviceDefaultDateFormat

Without it, I had some funny things happening with dates and times. I can't remember exactly what they were now, but I know that statement fixed them.
 
Upvote 0

iatall

Member
Licensed User
Longtime User
i want to get datetime in this format : "yyyyMMddHHmmss" --- year_month_date_hour_min_sec
is it possible?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Just do this:
B4X:
DateTime.DateFormat="yyyyMMddHHmmss"
Log(DateTime.Date(DateTime.Now))  'displays something like this: 20150301165804
 
Upvote 0
Top