显示小时和分钟的日期时间

时间:2014-05-07 08:59:32

标签: c# datetime

我正在尝试按如下方式显示日期时间Wednesday, 05 May 2014 21:25

我尝试了以下操作,但在使用ToLongDateString时,我没有时间,这是我的代码

 DateTime date = DateTime.Now;
 string formattedDate = date.ToLongDateString();
 string fDate = date.ToString("MMMM dd, yyyy,H:mm");
 Response.Write(formattedDate);

6 个答案:

答案 0 :(得分:6)

日期字符串不包含时间。这就是它调用 date 字符串的原因。这是您想要的格式:

DateTime date = DateTime.Now;
string formattedDate = date.ToString("dddd, dd MMMM yyyy HH:mm");
// Wednesday, 07 May 2014 12:05

答案 1 :(得分:1)

ToLongDateString不包含时间,因为时间不是日期的一部分 有关详细信息,请参阅HERE

  

当前文化:“en-US”

     

长日期模式:“dddd,MMMM dd,yyyy”长日期字符串:   “2001年5月16日星期三”

     

长时间模式:“h:mm:ss tt”长时间字符串:“3:02:15 AM”

     

短日期模式:“M / d / yyyy”短日期字符串:“5/16/2001”

     

短时间模式:“h:mm tt”短时间字符串:“3:02 AM”

对于ToString DateTime的所有可能性,HEREHERE也是如此。

您可能想要使用ToString("F")

  

“F”标准格式说明符表示自定义日期和时间   格式字符串,由当前定义   DateTimeFormatInfo.FullDateTimePattern属性。例如,   不变文化的自定义格式字符串是“dddd,dd MMMM yyyy   HH:MM:SS,”

答案 2 :(得分:0)

您需要使用字符串dddd, dd MMM yyyy HH:mm

 string fDate = DateTime.Now.ToString("ddddd, dd MMMM yyyy HH:mm");
 Response.Write(fDate );

此外,您的代码输出formattedDate而不是fDate值。

答案 3 :(得分:0)

试试这种方式

    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

了解更多详情,请访问以下页面 http://www.dotnetperls.com/datetime-format

答案 4 :(得分:0)

你可以尝试这个

DateTime date = DateTime.Now;
                string formattedDate = date.ToLongDateString();
                string fDate = date.ToString("dddd  MMMM dd, yyyy  hh:mm");
                Response.Write(fDate);

答案 5 :(得分:-1)

以下内容应该有效:

string formattedDate = date.ToLongDateString();
formattedDate += date.ToString(" h:mm");