Solved

Date difference in C#

Posted on 2006-06-27
10
344 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:claracruz
10 Comments
 
LVL 10

Expert Comment

by:orbulat
ID: 16990979
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;
}
0
 
LVL 4

Author Comment

by:claracruz
ID: 16991056
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
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16991575
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();
                  }
0
 
LVL 4

Author Comment

by:claracruz
ID: 16991701
I am using c# not VB
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Author Comment

by:claracruz
ID: 16991708
tried it, but doesn't work
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16992868
((Label)e.Item.FindControl("lblRegDate")).Text = Math.Abs(DateAndTime.DateDiff (DateInterval.Year ,nowDate,regDate,Microsoft.VisualBasic.FirstDayOfWeek.System, FirstWeekOfYear.System )).ToString();
0
 
LVL 4

Author Comment

by:claracruz
ID: 16994041
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
0
 
LVL 4

Author Comment

by:claracruz
ID: 17069044
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";
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 17105809
Closed, 500 points refunded.
Netminder
Site Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now