Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display date time with hours and minutes

Tags:

c#

datetime

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);
like image 848
Learner Avatar asked May 07 '14 08:05

Learner


People also ask

What formats for displaying date and time?

The dates appear as, mm/dd/yyyy in the U.S. and as, dd/mm/yyyy outside the U.S. where mm is the month, dd is the day, and yyyy is the year. The time is displayed as, hh:mm:ss AM/PM, where hh is the hour, mm is minutes, and ss is seconds.

How do I use 24 hour format in DateTime?

Two 'H' in HH is for 24-hour format. Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.

How do you format time?

4-digit year, month, day, 24-based hour, minute, second -- usually with leading zeroes. These six are the easiest formats to use and remember in Time::Format: yyyy , mm , dd , hh , mm , ss .

How do I print only hours and minutes in Python?

To get the current time in particular, you can use the strftime() method and pass into it the string ”%H:%M:%S” representing hours, minutes, and seconds.


2 Answers

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
like image 52
Sergey Berezovskiy Avatar answered Sep 30 '22 10:09

Sergey Berezovskiy


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

like image 38
Christoph Fink Avatar answered Sep 30 '22 10:09

Christoph Fink