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);
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);
source share