Link to home
Start Free TrialLog in
Avatar of claracruz
claracruz

asked on

Date difference in C#

Helllo experts,

I need to display the difference between the year of a  date displayed from the database;-

<%# Eval("DATEOFREGISTRATION" , "{0:dd-MM-yyyy}") %>

 and the year for now date. The difference will be displayed in years..
How do I do this?, I am displaying in a datalist
Avatar of orbulat
orbulat
Flag of Hong Kong image

I think it's better to use DataList ItemDataBound Event

then you parse the Date from the Database

private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
  DateTime regDate = DateTime.Parse(((DataRowView)e.Item.DataItem)["DateOfRegistration"].ToString()));
  DateTime nowDate = DateTime.Now;

  TimeSpan tp = nowDate.SubStract(regDate);

  Label lblRegDate = (Label)e.Item.FindControl("lblRegDate");
  lblRegDate.Text = tp.TotalDays() / 365;
}
Avatar of claracruz
claracruz

ASKER

Hi there,

Cheers for that.. Hiowever am not getting any value for the label.. I made a few changes as I was getting errors;-

private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {
      DateTime regDate = DateTime.Parse(((DataRowView)e.Item.DataItem)["DateOfRegistration"].ToString());
      DateTime nowDate = DateTime.Now;
      double dateResult;

      TimeSpan tp = nowDate.Subtract(regDate);

      Label lblRegDate = (Label)e.Item.FindControl("lblRegDate");
      dateResult = tp.TotalDays / 365;
      lblRegDate.Text = dateResult.ToString();
    }

Have the amendments caused any problems. Am not getting erros, but the date is not being displayed in the label

cheerrs
One line code

//using Microsoft.VisualBasic;
               //Add a reference to Microsoft.VisualBasic (Microsoft Visual Basic .NET Runtime)

                  private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
                  {
                        DateTime regDate = DateTime.Parse(((DataRowView)e.Item.DataItem)["DateOfRegistration"].ToString());
                        DateTime nowDate = DateTime.Now;
                        Label lblRegDate = (Label)e.Item.FindControl("lblRegDate");
                        lblRegDate.Text = Math.Abs(DateAndTime.DateDiff (DateInterval.Year ,nowDate,regDate,Microsoft.VisualBasic.FirstDayOfWeek.System, FirstWeekOfYear.System )).ToString();
                  }
I am using c# not VB
tried it, but doesn't work
((Label)e.Item.FindControl("lblRegDate")).Text = Math.Abs(DateAndTime.DateDiff (DateInterval.Year ,nowDate,regDate,Microsoft.VisualBasic.FirstDayOfWeek.System, FirstWeekOfYear.System )).ToString();
hi there,

I get the following error
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'DateAndTime' does not exist in the current context

Source Error:

 

Line 59:
Line 60:
Line 61:       ((Label)e.Item.FindControl("lblRegDate")).Text = Math.Abs(DateAndTime.DateDiff(DateInterval.Year, nowDate, regDate, Microsoft.VisualBasic.FirstDayOfWeek.System, FirstWeekOfYear.System)).ToString();
Line 62:
Line 63:     }
 
Is there no c# equivalent for doing the same thing
Hi Guys,

Finally gothe C# code for it;-
//BIND THE END OF TIMES
        Label lblTimeLeft = (Label)FormView1.FindControl("lblTimeLeft");
        DateTime dtTimeLeft1 = DateTime.Parse(lblTimeLeft.Text.ToString());
        DateTime dtTimeLeft2 = DateTime.Now;
        dtTimeLeft2 = dtTimeLeft2.AddDays(1);


        TimeSpan result = dtTimeLeft2.Subtract(dtTimeLeft1);
        lblTimeLeft.Text = result.Days + " days " + result.Hours + " hours " + result.Minutes + " minutes " + result.Seconds + " seconds";
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial