Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change timespan variable to a integer type?

Tags:

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.

like image 934
Aman Mehrotra Avatar asked May 17 '13 10:05

Aman Mehrotra


People also ask

What is TimeSpan datatype?

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.

How do I convert DateTime to TimeSpan?

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.

How do you get days from TimeSpan?

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.

What is C# TimeSpan?

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.


2 Answers

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; 
like image 105
Habib Avatar answered Sep 28 '22 02:09

Habib


 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

like image 40
Lawrence Wong Avatar answered Sep 28 '22 03:09

Lawrence Wong