Reputation:
I am creating a web app in asp.net in which I am fetching the time from my database through string and printing the string date format in my database is 2016-10-15 00:00:00.000
and when I print the string the date format is showing like 10-Oct-16 12:00:00 AM
but I just want to print 10-Oct-16
I tried
string reformattedDate = DateTime.ParseExact(txtdate, "dd-MM-yyyy", null)
.ToString("MMM d, yyyy");
This but it is me showing:
String was not recognized as a valid DateTime
Can anyone suggest me a better way?
Upvotes: 0
Views: 2000
Reputation: 2111
Your date is yyyy-MM-dd hh:mm:ss.fff
, the format you have inputted is only dd-MM-yyyy
.
This is causing the error. Change this to:
string reformattedDate = DateTime.ParseExact("2016-10-15 00:00:00.000", "yyyy-MM-dd hh:mm:ss.fff", CultureInfo.InvariantCulture)
.ToShortDateString();
EDIT: You can also use ToString("dd-MMM-yy")
instead of ToShortDateString()
to get the desired output.
e.g.:
string reformattedDate = DateTime.ParseExact("2016-10-15 00:00:00.000", "yyyy-MM-dd hh:mm:ss.fff", CultureInfo.InvariantCulture)
.ToString("dd-MMM-yy");
Upvotes: 2