Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output DateTime date but no time using only CultureInfo

Tags:

c#

datetime

My code:

dateObject.Value.ToString(Model.Culture)

dateObject is of type DateTime? while Model.Culture is of type CultureInfo.

The (culture dependent) output:

17/08/1960 00:00:00

I'd like to remove the 00:00:00 part without having to specify any format provider. Model.Culture should contains the format for the date already while somewhere else (but where and how?) I'd like to specify not to output any time.

I was thinking to cast DateTime over a Date object but, unfortunately, there is no such object in C#. Hardcoding the date format directly will result in the impossibility to create a dynamic and culture dependent program.

like image 427
Saturnix Avatar asked Aug 21 '13 09:08

Saturnix


People also ask

How can I get only the date from DateTime format?

the easiest way to reach ONLY date is the simple function in Datetime: DateTime dt = DateTime. now; String BirthDate = dt. ToShortDateString();

What is T and Z in time format?

The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC). If your strings always have a “Z” you can use: SimpleDateFormat format = new SimpleDateFormat( “yyyy-MM-dd'T'HH:mm:ss).

What is FFF in date format?

The "fff" custom format specifier represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value.


1 Answers

" without having to specify any format provider" ... "Hardcoding the date format directly will result in the impossibility to create a dynamic and culture dependent program."

You can use the overload that enables to provide a format string and the culture:

string result = dateObject.Value.ToString("d", Model.Culture);

or you change the culture if that is desired/possible:

System.Threading.Thread.CurrentThread.CurrentCulture = Model.Culture;
string result = dateObject.Value.ToShortDateString();
like image 184
Tim Schmelter Avatar answered Oct 02 '22 05:10

Tim Schmelter