locked
linqtosql timespan help RRS feed

  • Question

  • Hi all I am using linqtoSQL with a winforms app in VS2010.

    I am struggling to find the timespan in 2 minutes between 2 give datetimes.

     

    In my sql database the column is set to datetime and the information in there is in thi format

    2011-09-13 12:02:37.000

    I use thw following code to try calculate the timespan in my form

    private

     

     

     

    void FinalDoc_Load(object sender, EventArgs e)        {            label6.Text = Data.docnum;using (DocketsDataContext context = new DocketsDataContext())            {var dockets = from c in context.Docketswhere (c.DocketNum == label6.Text.Trim())select c;foreach (var docket in dockets)                {                    label8.Text = docket.TimeRaised.ToString();            //docket.TimeRaised)ToString("yyyy-MM-dd HH:mm:ss");                    labelstart.Text = docket.EngStart.Trim();//label8.Text = DateTime.Parse(docket.TimeRaised).ToString("yyyy-MM-dd HH:mm:ss");//labelstart.Text = DateTime.Parse(docket.EngStart).ToString("yyyy-MM-dd HH:mm:ss"); labeleng.Text = docket.EngName.Trim();                    catnum.Text = docket.Category;                    subnum.Text = docket.SubCategory;                    labelfin.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//(DateTime.Now).ToString();  //(DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss")var Date1 = DateTime.Parse(label8.Text);var Date2 = DateTime.Parse(labelfin.Text);var dateDifference = Date2.Subtract(Date1).Duration();int minutes = (int) Math.Round(dateDifference.TotalMinutes);                    label10.Text = minutes.ToString();                }            }        }

     

    But my label 10 fails to give the duration, your help is greatly appreciated

     

    Jay

    Tuesday, September 13, 2011 2:01 PM

Answers

  • So this line of code fails:

     var Date1 = DateTime.Parse(label8.Text);
    

    Try with:

    public Form1()
            {
                InitializeComponent();
                label1.Text = DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss");
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                DateTime d1 = DateTime.Parse(label1.Text);
                DateTime d2 = DateTime.Now;
    
                TimeSpan ts = d2.Subtract(d1);
                int seconds = ts.Seconds;
            }
    

    I changesd the code to get seconds, and it works.

     


    Mitja
    • Marked as answer by Martin_Xie Tuesday, October 11, 2011 10:10 AM
    Tuesday, September 13, 2011 3:53 PM

All replies

  • I dont no what happend to my code 

    private void FinalDoc_Load(object sender, EventArgs e)
            {
                label6.Text = Data.docnum;
                using (DocketsDataContext context = new DocketsDataContext())
                {
                    var dockets = from c in context.Dockets
                                  where (c.DocketNum == label6.Text.Trim())
                                  select c;
                    foreach (var docket in dockets)
                    {
                        label8.Text = docket.TimeRaised.ToString();            //docket.TimeRaised)ToString("yyyy-MM-dd HH:mm:ss");
                        labelstart.Text = docket.EngStart.Trim();
                        //label8.Text = DateTime.Parse(docket.TimeRaised).ToString("yyyy-MM-dd HH:mm:ss");
                        //labelstart.Text = DateTime.Parse(docket.EngStart).ToString("yyyy-MM-dd HH:mm:ss");
    
    
                        labeleng.Text = docket.EngName.Trim();
                        catnum.Text = docket.Category;
                        subnum.Text = docket.SubCategory;
                        labelfin.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            //(DateTime.Now).ToString();  //(DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss")
    
                        var Date1 = DateTime.Parse(label8.Text);
                        var Date2 = DateTime.Parse(labelfin.Text);
                        var dateDifference = Date2.Subtract(Date1).Duration();
                        int minutes = (int) Math.Round(dateDifference.TotalMinutes);
                        label10.Text = minutes.ToString();
                    }
    
                }
            }
    


    Tuesday, September 13, 2011 2:02 PM
  • Do it this way:

                DateTime d1 = DateTime.Now;
                DateTime d2 = d1.AddMinutes(3);
                TimeSpan ts = d2.Subtract(d1);
                int minDiff = ts.Minutes; //will be 3 minutes
    

    just make sure your labels holds the actual dates.


    Mitja
    Tuesday, September 13, 2011 2:10 PM
  •                     labeleng.Text = docket.EngName.Trim();
                        catnum.Text = docket.Category;
                        subnum.Text = docket.SubCategory;
                        labelfin.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            //(DateTime.Now).ToString();  //(DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss")
    
                        var Date1 = DateTime.Parse(label8.Text);
                        var Date2 = DateTime.Now;
    
                        TimeSpan ts = Date2.Subtract(Date1);
                        //var dateDifference = Date2.Subtract(Date1).Duration();
                        int minutes = (int) Math.Round(ts.TotalMinutes);
                        label10.Text = minutes.ToString();
    
    Ok I tired the above and in debugging the labels hold there time and date but once it gets to the timespan they become Null
    Tuesday, September 13, 2011 2:53 PM
  • So this line of code fails:

     var Date1 = DateTime.Parse(label8.Text);
    

    Try with:

    public Form1()
            {
                InitializeComponent();
                label1.Text = DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss");
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                DateTime d1 = DateTime.Parse(label1.Text);
                DateTime d2 = DateTime.Now;
    
                TimeSpan ts = d2.Subtract(d1);
                int seconds = ts.Seconds;
            }
    

    I changesd the code to get seconds, and it works.

     


    Mitja
    • Marked as answer by Martin_Xie Tuesday, October 11, 2011 10:10 AM
    Tuesday, September 13, 2011 3:53 PM