Results 1 to 8 of 8

Thread: Textmode=Time not displaying AM/PM

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2025
    Posts
    3

    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???

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,066

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2025
    Posts
    3

    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!

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,084

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,066

    Re: Textmode=Time not displaying AM/PM

    Quote Originally Posted by dday9 View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,066

    Re: Textmode=Time not displaying AM/PM

    Quote Originally Posted by twd76 View Post
    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".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,084

    Re: Textmode=Time not displaying AM/PM

    Quote Originally Posted by jmcilhinney View Post
    I think you might be referring to the WinForms DateTimePicker control but this is a Web Forms project.
    You are correct, thank you.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2025
    Posts
    3

    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
  •  



Click Here to Expand Forum to Full Width