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);
有帮助吗?

解决方案

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

其他提示

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".

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.

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

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);

This format should work:

DateTime date = DateTime.Now;
string formattedDate = date.ToString("f"); 
// January 13, 2023 5:00 PM

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...

The following should work:

string formattedDate = date.ToLongDateString();
formattedDate += date.ToString(" h:mm");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top