I'm trying to convert timespan variable into an integer variable using 'parse'. I get an error that says:
Format exception was unhandled: Input string was not in correct format
This is the code is have :
private void dateTimePicker4_ValueChanged(object sender, EventArgs e) { TimeSpan t = dateTimePicker4.Value.ToLocalTime() - dateTimePicker3.Value.ToLocalTime(); int x = int.Parse(t.ToString()); y = x; }
My target is to display this the change in time for two timepickers, dynamically in a text box, i.e, the difference in minutes between them should be displayed in a textbox automatically.
TimeSpan (amount of time) is a new data type that can be used to store information about an elapsed time period or a time span. For example, the value in the picture (148:05:36.254) consists of 148 hours, 5 minutes, 36 seconds, and 254 milliseconds.
To convert a DateTime to a TimeSpan you should choose a base date/time - e.g. midnight of January 1st, 2000, and subtract it from your DateTime value (and add it when you want to convert back to DateTime ). If you simply want to convert a DateTime to a number you can use the Ticks property. Save this answer.
A TimeSpan value can be represented as [-]d. hh:mm:ss. ff, where the optional minus sign indicates a negative time interval, the d component is days, hh is hours as measured on a 24-hour clock, mm is minutes, ss is seconds, and ff is fractions of a second. The value of the Days property is the day component, d.
C# TimeSpan struct represents a time interval that is difference between two times measured in number of days, hours, minutes, and seconds. C# TimeSpan is used to compare two C# DateTime objects to find the difference between two dates.
the difference in minutes between them should be displayed in a textbox automatically.
Instead of parsing use TimeSpan.TotalMinutes
property.
t.TotalMinutes;
The property is of double type, if you just need to integer part then you can do:
int x = (int) t.totalMinutes;
private void dateTimePicker4_ValueChanged(object sender, EventArgs e) { TimeSpan t = dateTimePicker4.Value.ToLocalTime() - dateTimePicker3.Value.ToLocalTime(); int x = int.Parse(t.Minutes.ToString()); y = x; }
Have you tried changing it to int x = int.Parse(t.Minutes.ToString());
?
From : http://msdn.microsoft.com/en-us/library/system.timespan.aspx
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