Android Question Formatting date and time into a string

nibbo

Active Member
Licensed User
Longtime User
I want to make a string = to the current date and time in the format yyyy-MM-dd HH:mm:ss
Simple enough in VB but not quite so in B4A.
I can do the code below...
B4X:
Dim sNow As String = DateTime.getyear(DateTime.Now) & "-" & DateTime.GetMonth(DateTime.Now) & "-" & DateTime.GetDayOfMonth(DateTime.Now) & " " & DateTime.GetHour(DateTime.Now) & ":" & DateTime.getminute(DateTime.Now) & ":" & DateTime.GetSecond
But that seems a little long winded. I am guessing I use DateTime.DateFormat but I can't seem to get it to work.

Thanks in advance
 

DonManfred

Expert
Licensed User
Longtime User
Look at documentation (or for examples) of using DateTime.DateFormat and DateTime.Timeformat
 
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
Err... I did and could not find an example that worked, that's why I am asking!
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
If you looked at the documentation and still need help, here is your solution:
B4X:
Dim OrigFormat As String=DateTime.DateFormat  'save orig date format
DateTime.DateFormat="yyyy-MM-dd HH:mm:ss"
Dim MyDate As String=DateTime.Date(DateTime.Now)
Msgbox(MyDate,"")
DateTime.DateFormat=OrigFormat  'return to orig date format
 
Upvote 0
Top