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
 

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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…