Solved

Date difference in C#

Posted on 2006-06-27
10
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:claracruz
ID: 16991701
I am using c# not VB
0
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

735 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