Reputation: 1911
I have a collection of data in my database, time and description, choose String as data type of my time, my question is how can I format my time data to this format "HH:MM AM/PM"
The time is "4:45 PM", I want to format this into this format "04:45 PM" in C# but I do not know how can I format that.
here is my code:
var str = touritinerary.Model.Time; //"4:45 PM"
var timePattern = "h:mm";
DateTime finalizeTime;
if (DateTime.TryParseExact(str, timePattern, null, DateTimeStyles.None, out finalizeTime))
{
Console.WriteLine("Time: {1:hh:mm }", finalizeTime);
}
I want 4:45 PM formatted into 04:45 PM
Upvotes: 3
Views: 6888
Reputation: 32694
You will find this page incredibly useful, it has all the DateTime formatting info you need from MSDN.
Console.WriteLine("Time: {0: hh:mm tt}", finalizeTime);
Upvotes: -3
Reputation: 74257
You read the documentation:
Try
string time = DateTime.Now.ToString("hh:mm tt") ;
Upvotes: 4
Reputation: 66449
You're close.
0
instead of 1
, since finalizeTime
is the first (and only) argument to WriteLine
.tt
to display PM
.Try this:
Console.WriteLine("Time: {0:hh:mm tt }", finalizeTime);
Output:
Time: 04:45 PM
Upvotes: 6