How to convert 1400-1500 to 2 pm - 3 hours?

I get data representing the time interval from the service as a string in the form:

1500-1600

This value is from 3 pm to 4 pm.

I will have a list of these, for example,

1200-1300

1300-1400

1400-1500

and I have to present this in the user interface as

12pm - 1pm

1pm - 2pm

2pm - 3pm

Unfortunately, this list may be in random order.

My question is, is there a way to use a DateTime object to be able to convert 24-hour time to 12-hour time, and is there also a way to arrange the time in order?

At the moment, I feel that I will have to write my own parsing function, but I wonder if anyone knows how to do this better? or could advise how they will achieve this.

+3
5

(1200, 1300), (, 9 - 0900), ParseExact DateTime:

DateTime dt = DateTime.ParseExact(time, "HHmm", formatInfo);

formatInfo IFormatProvider .

, :

DateTime dt = DateTime.ParseExact(time, "Hmm", formatInfo);

- , , "121".

, am/pm .. .., ToString, :

string ampm = dt.ToString("htt");
+4

, , "1400" "1500". () -

DateTime.ParseExact(input, "HHmm", CultureInfo.InvariantCulture)

DateTime . DateTime, .

"1400" , , Math.DivRem, 100. DateTime, .

+11

"-", , DateTime.ParseExact() datetime

+1

,

Dim aDateTime As DateTime
aDateTime = DateTime.ParseExact(input, "HHmm", Nothing)
Dim outputToDesiredFormat As String = dt.ToString("h:mm tt")
0

Custom parsing should be trivial. Divide by "-" and analyze each time; delete the last two digits to get the minutes, the remaining one or two to get the hour, and you're done.

If you need them, collect them.

Please update your question or leave a comment if you need further assistance.

-1
source

Source: https://habr.com/ru/post/1705192/


All Articles