How to convert a Date to a formatted string in VB.net?

后端 未结 5 433
感情败类
感情败类 2020-12-17 09:13

There seems to be a million questions here on converting a string to a Date, but not vice-versa. When I convert a Date object to a string using mydate.toString

相关标签:
5条回答
  • 2020-12-17 10:02

    you can do it using the format function, here is a sample:

    Format(mydate, "yyyy-MM-dd HH:mm:ss")
    
    0 讨论(0)
  • 2020-12-17 10:06
    Dim timeFormat As String = "yyyy-MM-dd HH:mm:ss"
    objBL.date = Convert.ToDateTime(txtDate.Value).ToString(timeFormat)
    
    0 讨论(0)
  • 2020-12-17 10:07
    myDate.ToString("yyyy-MM-dd HH:mm:ss")
    

    the capital HH is for 24 hours format as you specified

    0 讨论(0)
  • 2020-12-17 10:08

    I like:

    Dim timeFormat As String = "yyyy-MM-dd HH:mm:ss"
    myDate.ToString(timeFormat)
    

    Easy to maintain if you need to use it in several parts of your code, date formats always seem to change sooner or later.

    0 讨论(0)
  • 2020-12-17 10:16

    You can use the ToString overload. Have a look at this page for more info

    So just Use myDate.ToString("yyyy-MM-dd HH:mm:ss")

    or something equivalent

    0 讨论(0)
提交回复
热议问题