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

泄露秘密 提交于 2019-12-11 00:18:57

问题


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")


来源:https://stackoverflow.com/questions/40389211/vb-net-custom-mask-for-date-time-am-pm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!