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.
the easiest way to reach ONLY date is the simple function in Datetime: DateTime dt = DateTime. now; String BirthDate = dt. ToShortDateString();
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).
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.
" 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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With