Solved

Date difference in C#

Posted on 2006-06-27
10
342 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

17 Experts available now in Live!

Get 1:1 Help Now