-2

I need to convert the datetime that is 24 hour format, and have to show it as 12 hour format. I got answers as , convert it into string as 12 hr format and show as string.

But I need to show as date time format. So i have used Convert.ToDateTime(datetime), but it again change datetime into 24 hour format , any better way to convert 12 hour format ?

Thanks in advance.

Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43
Riyas
  • 475
  • 1
  • 8
  • 27
  • 8
    Your question is confused and confusing. A `DateTime` has no format. Only when you ask for it to be converted to a string (or convert strings to `DateTime`) do formats come into play, but it's not clear how, in your case. Give more details on your input, your intended output, and what you're doing now that doesn't satisfy you. – Jeroen Mostert Mar 05 '18 at 12:10
  • https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings specifically the "h" specifier – Alexandru Clonțea Mar 05 '18 at 12:11
  • Refer this link [link](https://stackoverflow.com/questions/8337625/convert-am-pm-time-to-24-hours-format) – Manpreet Matharu Mar 05 '18 at 12:13
  • @JeroenMostert According to date time, it taken from the device time, some device has 12 hr format and some has 24 hour format, for me its not suitable to calculate further for my application, so i have to customize the time into one format, thats the case ! – Riyas Mar 05 '18 at 12:20
  • 1
    Show *code* of how you're getting "the device time". `DateTime.Now` has no format either -- it measures time in absolute ticks from a starting point. `DateTime.Now.ToString("s")` gives the current date and time in ISO 8601, which is independent of any settings. But, again, this is only relevant if you need to convert to a string -- otherwise, you should keep things as a `DateTime` for as long as possible. If you need to *parse* an AM/PM time string, that's a different question. – Jeroen Mostert Mar 05 '18 at 12:22
  • It sounds like you want to change the actual `DateTime` so that e.g. 2018-03-05T13:01:02 would become 2018-03-05T01:01:02. You could do that by seeing if `Hour` was 12 or greater and doing `AddHours(-12)` in that case, but tbh that approach seems like a likely bug-farm. – Jon Hanna Mar 05 '18 at 12:23
  • @Riyas if the device returns something with format, it returns a string. DateTime has no format. It's a binary value just like double or int. You have a *string* in some format that you want to parse. Post the strings you get from each "device" (what devices are these?) and the code you use to retrieve the values – Panagiotis Kanavos Mar 05 '18 at 12:39
  • @Riyas the value returned by `ToDateTime` or `DateTime.Parse` has no format either, since it's a DateTime. If you don't believe this, [check the source code](https://referencesource.microsoft.com/#mscorlib/system/datetime.cs,138). If you want to format the DateTime value using a specific format, use [DateTime.ToString](https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx) or `String.Format` and specify the format you want in the format string parameter, eg `DateTime.Now.ToString("hh:mm:ss")` – Panagiotis Kanavos Mar 05 '18 at 12:41

2 Answers2

1

Following MSDN (DateTime Structure) :

Internally, all DateTime values are represented as the number of ticks (the number of 100-nanosecond intervals) that have elapsed since 12:00:00 midnight, January 1, 0001. The actual DateTime value is independent of the way in which that value appears when displayed in a user interface element or when written to a file. The appearance of a DateTime value is the result of a formatting operation. Formatting is the process of converting a value to its string representation.

So a DateTime is just a number of ticks.

If you want to display the date, then you actually want to format this number into a human readable format.

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
0

Here is an example:

Action<string> display = Console.WriteLine;
DateTime now  = DateTime.Now;
display($"24 hour format: {now:F}"); // 13:31:20
display($"12 hour format: {now:hh:mm:ss}"); // 01:31:20
display($"24 hour format: {now:HH:mm:ss}"); // 13:31:20
Fred Smith
  • 2,047
  • 3
  • 25
  • 36