I have integer values like 06,07,08,.....,16,17,18,...
I want to convert this integer values to 24 hour time format.
I am 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 it is giving Error
"String was not recognized as a valid DateTime."
while parsing.
What I am doing wrong here. ?
To convert number INT in minutes to TIME in MySQL, you can use SEC_TO_TIME() function. Insert some records in the table using insert command.
int val = 16; TimeSpan result = TimeSpan. FromHours(val); string fromTimeString = result. ToString("hh':'mm");
To convert time to seconds, multiply the time time by 86400, which is the number of seconds in a day (24*60*60 ).
We can convert an integer to the character by adding a '0' (zero) character. The char data type is represented as ascii values in c programming. Ascii values are integer values if we add the '0' then we get the ASCII of that integer digit that can be assigned to a char variable.
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 correct type to represent your need, as you only care about the time within a day. A date also represents the day, and you cannot truncate it (as far as I know).
See TimeSpan.FromHours Method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With