Android Question Date and Time

gpbimal

Member
Hi! I'm new for b4a, i'm trying to get date and time in a string but unable.
here is my code

Dim DTStr As String
DateTime.DateFormat="mm-dd"
DTStr = DateTime.Date(DateTime.Now)
DateTime.TimeFormat="hh:mm:ss a"
DTStr = DTStr & " [" & DateTime.Time(DateTime.Now) & "]"

Please correct me
 

Mahares

Expert
Licensed User
Longtime User
Please correct me
First of all, what does the string you want to achieve look like. Give an example. There are mistakes in your code. For example the date format you have is for minutes and day not month and day. Please use code tags when posting code
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
There's really only one error. To see date/time formats, hover over .DateFormat and click on 'formats'.
B4X:
    DateTime.DateFormat="MM-dd"
 
Upvote 0

gpbimal

Member
Thank you William, problem is solved.

B4X:
    Dim DTStr As String
    DateTime.DateFormat="yyyy-MM-dd"
    DTStr = DateTime.Date(DateTime.Now)
    DateTime.TimeFormat="hh:mm:ss a"
    DTStr = DTStr & " [" & DateTime.Time(DateTime.Now) & "]"
    Log(DTStr)

Now the result is: 2022-09-27 [03:19:49 PM]
 
Upvote 0

gpbimal

Member
Since now you are posting how you want your string to look like, you can simplify your code like this:
B4X:
DateTime.DateFormat="yyyy-MM-dd [h:mm:ss a]"
Dim DTStr As String = DateTime.Date(DateTime.Now)
Log(DTStr)  '2022-09-27 [10:59:36 AM]
Wow...Superb!
Thank you Mahares, I was stupid that I unable to share my requirement (exactly what I need)
 
Upvote 0
Top