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
Solved

Date difference in C#

Posted on 2006-06-27
10
353 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Gridview selected row 9 49
ASP.net Show "please wait " message as page loads 2 63
Function after success of Jquery/AJAX action 9 27
FInd Image Control Gridview 3 19
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…

790 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