VB.Net Custom Mask for Date/Time AM/PM

2019-07-18 06:42发布

问题:

Here's what I'm trying to do.... I have a field in my application where I am to capture datetime something like this 11/02/2016 12:19 PM (example). I have a field in SQL Server with datatype = smallDatetime. It gets saved like this 2016-11-02 12:19:00. In my app I set the CUSTOM mask to 00/00/0000 90:00 am. Now what I'm trying to do is populate this saved date into my masked textbox but it does not look proper. Is there a way for me to format is somehow so when I try to populate the field in my application it looks properly? This is what I've been trying to figure out...

If Not IsDBNull(Dt("SavedOn")) Then
  txtSavedOn.Text = Format(Dt("SavedOn"), "mm/dd/yyyy hh:mm tt"))
End If

Wha thappens is the PM/AM part gets displayed incorrectly and it only shows the M and a number instead of p/a

回答1:

Change your mask to

00/00/0000 90:00 aa

Also your date format

txtSavedOn.Text = Format(Dt("SavedOn"), "MM/dd/yyyy hh:mm tt"))

(note the capital MM for month, opposed to lower mm for minute)



回答2:

Just use this code

 txtSavedOn.Text =  Dt("SavedOn").tostring("MM/dd/yyyy hh:mm tt")