How to convert Int while in C #?

I have integer values ​​like 06.07.08, ....., 16,17,18, ...

I want to convert these integer values ​​to a 24 hour time format.

I'm doing something like this

//fromTime holds one of the integer value. DateTime fromTimeDate = DateTime.ParseExact(fromTime.ToString(), "HH", CultureInfo.InvariantCulture); string fromtimestring = fromTimeDate.ToString("hh:mm tt"); 

But he gives an error

"The string was not recognized as a valid DateTime."

during parsing.

What I'm doing wrong here.

+6
source share
7 answers

Please try this code:

 int val = 16; TimeSpan result = TimeSpan.FromHours(val); string fromTimeString = result.ToString("hh':'mm"); 

Actually, I don't think DateTime is the right type to represent your need, since you only care about time during the day. A date also represents a day, and you cannot truncate it (as far as I know).

See TimeSpan.FromHours Method

+18
source

This can happen only for two reasons.

  • First, your fromTime stores a single integer, from 0 to 9 , and since its 9 not 09 it will not work with the HH format.
  • The second reason may be that the integer is greater than 23 or less than 0 .

You can change your format to one H digit, which will work for both.

 DateTime fromTimeDate = DateTime.ParseExact(fromTime.ToString(), "H", CultureInfo.InvariantCulture); //fromTime holds one of the integer value. 

The above should analyze any number from 0 to 23 .

By the way, it looks more like TimeSpan , then DateTime , you can convert it to TimeSpan using the TimeSpan.FromHours method,

 int fromTime = 23; TimeSpan ts = TimeSpan.FromHours(fromTime); 
+3
source

The easiest way is to do something like:

 DateTime time = Date.Today + new TimeSpan (0, 0, seconds); 
+3
source

The β€œHH” format needs two digits, and 09 as a whole is just β€œ9”.

Use fromTime.ToString("00") to force the leading 0.

Please note that the date-date of this DateTime will be today.

+3
source

The code you wrote is correct, please check once ...........

Values

"fromTime" can have one digit, for example, "6" instead of "06" or "0" or "Another number is more than 24"

"The string was not recognized as a valid DateTime." You will get this error when using "hh" instead of "HH" in the line below ..............

 DateTime fromTimeDate = DateTime.ParseExact(fromTime.ToString(), "HH", CultureInfo.InvariantCulture); 
+1
source

If you actually have an int representing the clock, you don't need to figure it out at all:

 int hour = 23; var today = DateTime.Today; var time = new DateTime(today.Year, today.Month, today.Day, hour, 0, 0); 

Or (slightly modified version of @ gean08 answer above):

 var time = DateTime.Today + TimeSpan.FromHours(hour); 
+1
source
  public string timeformat(int time) { string hr, min; min = Convert.ToString(time % 100); hr = Convert.ToString(time / 100); if (hr.Length == 1) hr = "0" + hr; if (min.Length == 1) min = "0" + min; return hr + ":" + min; } 
+1
source

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


All Articles