I need to get the difference of two date time picker values and convert it to int or float,

And the next thing is from the date time picker i cant select the time from front end.
Only i cant select is the time only.

these the some code that i have tried to convert values from date time picker to float,

But i don't have any idea to select the time from date time picker,

string d1 = DateTimePickerFormat.Time();
            string d2 = dateTimePicker2.ToString();
            //DateTime d1 = new DateTimePicker1();
            //DateTime d2 = new DateTimePicker();

            int ans = (int.Parse(d1) - int.Parse(d2));

            textBox1.Text = ans.ToString();

Recommended Answers

All 9 Replies

im with visual studio 2008 c#

What do you mean to get the difference between two dates in the integer type?
the number in minutes, seconds, hours, or something else?

To get the difference you have to use TimeSpan class, which has all the time values (seconds, minutes, hours, milliseconds,..).
You can do it:

DateTime t1 = dateTimePicker1.Value;
DateTime t2 = dateTimePicker2.Value;
//now lets assume that t1 is older date:
TimeSpan ts = t2.Subtract(t1);
int minutes = ts.Minutes; // here you get the difference between t1 and t2 in minutes
//the same you can do for other values of time (millinsends, hours,...)

About your 2nd question, i didnt really understand it (that you cant select it the front end) - What is that suppose to mean?
Look how I did in the example, and try to do it the same way.

DateTimePicker has a Value property which gives you a DateTime object.
From this Value you can extract Hour,Minute and Second values.
Alternatively you could use the DateTime ToString method with the appropriate format string to extract what you want.Example: http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

Thanxx guyssssssssss. will try ASAP and let u gys let know :D :D :D.
Thaxxxxxxxxxxxxx

dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "MM/dd/yyyy hh:mm:ss";

This is what i found, and its working fine

DateTime d1 = dateTimePicker1.Value;
            DateTime d2 = dateTimePicker2.Value;

            TimeSpan timeSpan = d2.Subtract(d1);

         //   TimeSpan timeSpan = d3.Subtract(d4);



            string ans2 = timeSpan.ToString();

            textBox1.Text = ans2;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "MM/dd/yyyy hh:mm:ss";

            dateTimePicker2.Format = DateTimePickerFormat.Custom;
            dateTimePicker2.CustomFormat = "MM/dd/yyyy hh:mm:ss"; 
        }

this is my code and unable to get the different bitween d1 and d2 as a pure float or a int value

Ups, i did a mistake on last line of code, to get total minutes you have to do:

int minutes = ts.TotalMinutes; // here you get the difference between t1 and t2 in minutes
//the same you can do for other values of time (millinsends, hours,...)

About your last post:
You can NOT parse TimeSpan to Stirng!!!

Tell me, what value you want to get out, seconds, minutes, hours, what?

Look this example of mine here:

int minutes = ts.TotalMinutes;
//to get a string you only do:
string strTotalMinutes = ts.TotalMinutes.ToString();

GUyss, im done with that... thanxx for the help... this can be marked as resolved .:d

textBox5.Text = hurs.ToString();
textBox1.Text = min.ToString();
textBox6.Text = sec.ToString();
textBox7.Text = ((hurs * 60) + min).ToString();

Yes, but you have to use a referece of TimeSpan object, like:

textBox5.Text = ts.Hours.ToString();
//and so on...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.