By default DateTime type in C# gave time in AM/PM format, but I would like to return 24 hrs time format and same time I want to return this DateTime type instead of string like below,
DateTime localTime = DateTime.Now;
string timeString24Hour = localTime.ToString("dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture) ;
instead of "string timeString24Hour" I want "DateTime timeString24Hour"
How to achieve this???
DateTime
does not have a format. It just has a value that represents a date and time. Formats only come into play when you convert it to astring
representation. And you are usingHH
which should give you a 24 hour value, so I don't see what your current issue is, though you should useMM
for month becausemm
is minutes.DateTime
in a specific format. ADateTime
is just a number. It has no concept of format - until you format it, at which point the result is not aDateTime
.