-3

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???

2
  • 1
    First of 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 a string representation. And you are using HH which should give you a 24 hour value, so I don't see what your current issue is, though you should use MM for month because mm is minutes.
    – juharr
    Sep 8, 2016 at 12:56
  • 5
    It is meaningless to ask for a DateTime in a specific format. A DateTime is just a number. It has no concept of format - until you format it, at which point the result is not a DateTime. Sep 8, 2016 at 12:56

3 Answers 3

2
DateTime localTime = DateTime.Now;

That is a DateTime type in 24-hour format. A DateTime is a number representing all of the milliseconds since January 1, 1970. From it, you can convert to any part of time, or any string of time. If you want to use a DateTime, though - there is no inherent formatting to it until you need one by using

DateTime.Hours/DateTime.DayOfWeek/DateTime.Subtract(antoherDateTime).TotalSeconds

if/when you need time in a 24- hour format. Just use:

DateTime.ToString("HH:mm") 

from here: How to format DateTime to 24 hours time?

4
  • 3
    a DateTime type isn't in any "format". It is a number. Just a number. Sep 8, 2016 at 12:55
  • And OP is asking for a DateTime. Datetime is a number that contains all of the time information necessary to do anything. If you can think of a way to convert a DateTime into "24 hour" format while still keeping it a DateTime, then you should vote me down. Please read question. OP asked FOR A DATETIME Sep 8, 2016 at 12:57
  • DateTime.ToString() by itself does not guarantee that the string will have a 24 hour format instead of using AM PM.
    – juharr
    Sep 8, 2016 at 13:02
  • Give me 10 seconds to insert my source, please. Sep 8, 2016 at 13:05
1

By default DateTime is dispalyed in AM/PM in your culture.

Because the developement environment always uses the current culture to display DateTime

enter image description here

But that's only how it is being displayed. The value within DateTime is the same - culture independent.

By th way "mm/dd/yyyy HH:mm:ss" is invalid becasue mm are minutes and MM is for month

3
  • The culture doesn't effect that HH will return a 24 hour format.
    – juharr
    Sep 8, 2016 at 13:06
  • @juharr right but I misunderstood the question, it's about DateTime and not string
    – fubo
    Sep 8, 2016 at 13:27
  • Personally I don't think the question is clear enough for an answer.
    – juharr
    Sep 8, 2016 at 13:37
0

I presume you want something like String.Format :

DateTime localTime = DateTime.Now;
string timeString24Hour = String.Format("{0:dd-MM-yyyy HH:mm:ss.fff}", localTime);

Or shorter:

string timeString24Hour = String.Format("{0:dd-MM-yyyy HH:mm:ss.fff}", DateTime.Now);

You can find informations here : MSDN: Custom Date and Time Format Strings

2
  • i want DateTime as return type
    – user584018
    Sep 8, 2016 at 13:18
  • instead of "string timeString24Hour" DateTime timeString24Hour
    – user584018
    Sep 8, 2016 at 13:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.