-
Mar 24th, 2025, 10:54 AM
#1
Thread Starter
New Member
Textmode=Time not displaying AM/PM
In my vb.net webforms application, I have a textbox with the Textmode = "Time". I need to show the correct AM/PM designation, so I am using the following code to populate the textbox:
txtTime.text = timeValue.ToString("hh:mm tt")
The timeValue in the database is "2025-03-23 17:00:00.000". When I use the "tt" parameter, nothing appears in the textbox.
If I use the following:
txtTime.text = timeValue.ToString("hh:mm") -- without the "tt" parameter
the textbox shows "05:00 AM", which is incorrect.
After adding a textbox labeled "txtTime" and setting TextMode=Time, I am running the following code which produces the results shown above:
Dim myTime As DateTime = "2025-03-23 17:00:00.000"
txtTime.Text = myTime.ToString("hh:mm tt")
Oddly enough, when debugging, the value shown in txtTime.Text is correct (05:00 PM)! However, the textbox does not show anything.
What am I missing???
-
Mar 24th, 2025, 11:45 AM
#2
Re: Textmode=Time not displaying AM/PM
Try using this:
Code:
txtTime.text = timeValue.ToString("HH:mm")
I'm not sure it will work but I think it might.
-
Mar 24th, 2025, 01:04 PM
#3
Thread Starter
New Member
Re: Textmode=Time not displaying AM/PM
omg ... it worked!!!
I felt like I had tried every conceivable combination of parameters, both upper and lower case, that I almost replied "I already tried that" before actually giving it a shot. In fact, the only reason I tried it was to show a screenshot of the failure. Imagine my surprise when it actually worked!!! I can't think you enough.
I'm still not sure why the "tt" parameter doesn't work, but that'll be an investigation for another day.
Thanks again!
-
Mar 24th, 2025, 01:15 PM
#4
Re: Textmode=Time not displaying AM/PM
This might be a situation where the correct control is actually the answer. If timeValue is a TimeStamp, DateTime, or DateTimeOffset then you really should be using a DateTimePicker control with the appropriate formatting.
For example, if its a TimeSpan:
Code:
Dim d As DateTime = New Date(timeValue.Ticks) ' convert to DateTime
d = d.AddDays(DateTime.Now.Subtract(DateTime.MinValue).Days) ' set date to min date, but keep time
DateTimePicker1.Format = DateTimePickerFormat.Time
DateTimePicker1.Value = d
Or if its a DateTime:
Code:
DateTimePicker1.Format = DateTimePickerFormat.Time
DateTimePicker1.Value = timeValue
-
Mar 24th, 2025, 01:40 PM
#5
Re: Textmode=Time not displaying AM/PM
 Originally Posted by dday9
This might be a situation where the correct control is actually the answer. If timeValue is a TimeStamp, DateTime, or DateTimeOffset then you really should be using a DateTimePicker control with the appropriate formatting.
For example, if its a TimeSpan:
Code:
Dim d As DateTime = New Date(timeValue.Ticks) ' convert to DateTime
d = d.AddDays(DateTime.Now.Subtract(DateTime.MinValue).Days) ' set date to min date, but keep time
DateTimePicker1.Format = DateTimePickerFormat.Time
DateTimePicker1.Value = d
Or if its a DateTime:
Code:
DateTimePicker1.Format = DateTimePickerFormat.Time
DateTimePicker1.Value = timeValue
I think you might be referring to the WinForms DateTimePicker control but this is a Web Forms project.
-
Mar 24th, 2025, 01:45 PM
#6
Re: Textmode=Time not displaying AM/PM
 Originally Posted by twd76
omg ... it worked!!!
I felt like I had tried every conceivable combination of parameters, both upper and lower case, that I almost replied "I already tried that" before actually giving it a shot. In fact, the only reason I tried it was to show a screenshot of the failure. Imagine my surprise when it actually worked!!! I can't think you enough.
I'm still not sure why the "tt" parameter doesn't work, but that'll be an investigation for another day.
Thanks again!
I think the AM/PM part is formatting applied by the control. By using "hh:mm tt" you were creating the string "05:00 PM" but the PM part was being ignored. All the control was seeing was "05:00" as a 24-hour time value, which is 5:00 AM as 12-hour time. I worked it out using this page:
https://www.w3schools.com/tags/tryit...nput_type_time
I edited the HTML on the left to change:
Code:
<input type="time" id="appt" name="appt">
to:
Code:
<input type="time" id="appt" name="appt" value="17:00">
and the control displayed "05:00 PM".
-
Mar 24th, 2025, 02:02 PM
#7
Re: Textmode=Time not displaying AM/PM
 Originally Posted by jmcilhinney
I think you might be referring to the WinForms DateTimePicker control but this is a Web Forms project.
You are correct, thank you.
-
Mar 24th, 2025, 02:13 PM
#8
Thread Starter
New Member
Re: Textmode=Time not displaying AM/PM
Thank you for the explanation ... that makes sense.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|