Reputation: 351
I am trying to display date time as follows Wednesday, 05 May 2014 21:25
I tried the following but when using ToLongDateString
I am not getting time, this is my code
DateTime date = DateTime.Now;
string formattedDate = date.ToLongDateString();
string fDate = date.ToString("MMMM dd, yyyy,H:mm");
Response.Write(formattedDate);
Upvotes: 6
Views: 6596
Reputation: 173
Personally, I like the format that just doing ToString() gives me e.g
HelperLib.LogMsg("Job Ran at + " + DateTime.Now.ToString();
// Job Ran at 21/01/2023 21:12:59
You can change the format with hh:mm if you don't want seconds as people have shown you above, however this format is exactly what I want and need. If I wanted the day name or month name I would use the formatting people have shown you above but for mew this gives me the Date and time and any variable that is a date format it works on e.g
DateTime raceDateTime = Convert.ToDateTime(RecordsetRow["RaceDateTime"]);
Console.WriteLine("racedatetime = " + raceDateTime.ToString();
and the same output...
Upvotes: 0
Reputation: 51
This format should work:
DateTime date = DateTime.Now;
string formattedDate = date.ToString("f");
// January 13, 2023 5:00 PM
Upvotes: 0
Reputation: 23093
ToLongDateString
does not contain the time, as the time is not part of the date.
See HERE for some details:
Current culture: "en-US"
Long date pattern: "dddd, MMMM dd, yyyy" Long date string: "Wednesday, May 16, 2001"
Long time pattern: "h:mm:ss tt" Long time string: "3:02:15 AM"
Short date pattern: "M/d/yyyy" Short date string: "5/16/2001"
Short time pattern: "h:mm tt" Short time string: "3:02 AM"
Also HERE and HERE on all the possiblities with ToString
for DateTime
.
You possibly want to use ToString("F")
:
The "F" standard format specifier represents a custom date and time format string that is defined by the current DateTimeFormatInfo.FullDateTimePattern property. For example, the custom format string for the invariant culture is "dddd, dd MMMM yyyy HH:mm:ss".
Upvotes: 3
Reputation: 161
Your can try this
DateTime date = DateTime.Now;
string formattedDate = date.ToLongDateString();
string fDate = date.ToString("dddd MMMM dd, yyyy hh:mm");
Response.Write(fDate);
Upvotes: 0
Reputation: 1
The following should work:
string formattedDate = date.ToLongDateString();
formattedDate += date.ToString(" h:mm");
Upvotes: -1
Reputation: 862
try this way
DateTime time = DateTime.Now; // Use current time
string format = "dddd, d MMM yyyy HH:mm"; // Use this format
Console.WriteLine(time.ToString(format)); // Write to console
for more details visit below page http://www.dotnetperls.com/datetime-format
Upvotes: 0
Reputation: 19407
You need to use the string dddd, dd MMM yyyy HH:mm
.
string fDate = DateTime.Now.ToString("ddddd, dd MMMM yyyy HH:mm");
Response.Write(fDate );
Also, your code is outputting formattedDate
not the fDate
value.
Upvotes: 0
Reputation: 236208
Date string does not include time. That's why it called date string. Here is your desired format:
DateTime date = DateTime.Now;
string formattedDate = date.ToString("dddd, dd MMMM yyyy HH:mm");
// Wednesday, 07 May 2014 12:05
Upvotes: 9