Reputation: 72570
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
I get a string in the format 16/01/2013 13:00:00
.
But what I really want is 2013-01-16 13:00:00
. I can't see any functions on the Date object that do this for me, do I need to use a regex or something instead?
Upvotes: 25
Views: 234893
Reputation: 661
Dim timeFormat As String = "yyyy-MM-dd HH:mm:ss"
objBL.date = Convert.ToDateTime(txtDate.Value).ToString(timeFormat)
Upvotes: 4
Reputation: 158
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.
Upvotes: 5
Reputation: 5433
myDate.ToString("yyyy-MM-dd HH:mm:ss")
the capital HH is for 24 hours format as you specified
Upvotes: 5
Reputation: 27342
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
Upvotes: 48
Reputation: 6159
you can do it using the format function, here is a sample:
Format(mydate, "yyyy-MM-dd HH:mm:ss")
Upvotes: 11