Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys,
i want to subtract to times; the actual time and a time in a dateTimePicker.
then i want to compare.

this is my actual code:

TimeSpan zero = new TimeSpan(0, 0, 0, 0, 0);

private void ti_timeLeft_Tick(object sender, EventArgs e)
{
            try
            {
                TimeSpan notification1 = new TimeSpan(0, 0, 2, 0, 0);
                TimeSpan notification2 = new TimeSpan(0, 0, 5, 0, 0);
                TimeSpan notification3 = new TimeSpan(0, 0, 10, 0, 0);
            }
            catch (Exception ex) { }

            DateTime now = DateTime.Now;
            List<TimeSpan> diffs = new List<TimeSpan>(7);

            #region "Monday"

            if (DateTime.Now.DayOfWeek.ToString() == "Monday")
            {
                if (dtp_monday1.Checked)
                {
                    if ((dtp_monday1.Value - now) > zero)
                        diffs.Add(dtp_monday1.Value - now);
                }
                if (dtp_monday2.Checked)
                {
                    if ((dtp_monday2.Value - now) > zero)
                        diffs.Add(dtp_monday2.Value - now);
                }
                if (dtp_monday3.Checked)
                {
                    if ((dtp_monday3.Value - now) > zero)
                        diffs.Add(dtp_monday3.Value - now);
                }
                if (dtp_monday4.Checked)
                {
                    if ((dtp_monday4.Value - now) > zero)
                        diffs.Add(dtp_monday4.Value - now);
                }
                if (dtp_monday5.Checked)
                {
                    if ((dtp_monday5.Value - now) > zero)
                        diffs.Add(dtp_monday5.Value - now);
                }
                if (dtp_monday6.Checked)
                {
                    if ((dtp_monday6.Value - now) > zero)
                        diffs.Add(dtp_monday6.Value - now);
                }
                if (dtp_monday7.Checked)
                {
                    if ((dtp_monday7.Value - now) > zero)
                        diffs.Add(dtp_monday7.Value - now);
                }

                if (diffs.Count == 0)
                {
                    //this.lab_time_left.Text = "";
                    return;
                }

                diffs.Sort();
                TimeSpan min = diffs[0];
                TimeSpan max = diffs[diffs.Count - 1];

                this.lab_time_left.Text = "Time left: " + min.Hours.ToString("00") + ":" + min.Minutes.ToString("00") + ":" + min.Seconds.ToString("00");
            }
}


how can i do this just with times:
if ((dtp_monday1.Value - now) > zero)

????

please help me
Posted

You can use for example DateTime.CompareTo Method[^] method to compare the dates. For example:
C#
...
if (dtp_monday1.Value.CompareTo(now) > 0) {
...
 
Share this answer
 
Try with below code:
C#
if((dtp_monday1.Value - now).TotalDays > 0)
{
	// Your logic
}

Here I used TotalDays to get total no of days difference between two dates. If you need for hours, minutes and seconds then use TotalHours, TotalMinutes, TotalSeconds.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900