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
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
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(ob ject sender, System.Web.UI.WebControls. DataListIt emEventArg s e)
{
DateTime regDate = DateTime.Parse(((DataRowVi ew)e.Item. DataItem)[ "DateOfReg istration" ].ToString ());
DateTime nowDate = DateTime.Now;
double dateResult;
TimeSpan tp = nowDate.Subtract(regDate);
Label lblRegDate = (Label)e.Item.FindControl( "lblRegDat e");
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
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(ob
{
DateTime regDate = DateTime.Parse(((DataRowVi
DateTime nowDate = DateTime.Now;
double dateResult;
TimeSpan tp = nowDate.Subtract(regDate);
Label lblRegDate = (Label)e.Item.FindControl(
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(ob ject sender, System.Web.UI.WebControls. DataListIt emEventArg s e)
{
DateTime regDate = DateTime.Parse(((DataRowVi ew)e.Item. DataItem)[ "DateOfReg istration" ].ToString ());
DateTime nowDate = DateTime.Now;
Label lblRegDate = (Label)e.Item.FindControl( "lblRegDat e");
lblRegDate.Text = Math.Abs(DateAndTime.DateD iff (DateInterval.Year ,nowDate,regDate,Microsoft .VisualBas ic.FirstDa yOfWeek.Sy stem, FirstWeekOfYear.System )).ToString();
}
//using Microsoft.VisualBasic;
//Add a reference to Microsoft.VisualBasic (Microsoft Visual Basic .NET Runtime)
private void DataList1_ItemDataBound(ob
{
DateTime regDate = DateTime.Parse(((DataRowVi
DateTime nowDate = DateTime.Now;
Label lblRegDate = (Label)e.Item.FindControl(
lblRegDate.Text = Math.Abs(DateAndTime.DateD
}
ASKER
I am using c# not VB
ASKER
tried it, but doesn't work
((Label)e.Item.FindControl ("lblRegDa te")).Text = Math.Abs(DateAndTime.DateD iff (DateInterval.Year ,nowDate,regDate,Microsoft .VisualBas ic.FirstDa yOfWeek.Sy stem, FirstWeekOfYear.System )).ToString();
ASKER
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 ("lblRegDa te")).Text = Math.Abs(DateAndTime.DateD iff(DateIn terval.Yea r, nowDate, regDate, Microsoft.VisualBasic.Firs tDayOfWeek .System, FirstWeekOfYear.System)).T oString();
Line 62:
Line 63: }
Is there no c# equivalent for doing the same thing
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
Line 62:
Line 63: }
Is there no c# equivalent for doing the same thing
ASKER
Hi Guys,
Finally gothe C# code for it;-
//BIND THE END OF TIMES
Label lblTimeLeft = (Label)FormView1.FindContr ol("lblTim eLeft");
DateTime dtTimeLeft1 = DateTime.Parse(lblTimeLeft .Text.ToSt ring());
DateTime dtTimeLeft2 = DateTime.Now;
dtTimeLeft2 = dtTimeLeft2.AddDays(1);
TimeSpan result = dtTimeLeft2.Subtract(dtTim eLeft1);
lblTimeLeft.Text = result.Days + " days " + result.Hours + " hours " + result.Minutes + " minutes " + result.Seconds + " seconds";
Finally gothe C# code for it;-
//BIND THE END OF TIMES
Label lblTimeLeft = (Label)FormView1.FindContr
DateTime dtTimeLeft1 = DateTime.Parse(lblTimeLeft
DateTime dtTimeLeft2 = DateTime.Now;
dtTimeLeft2 = dtTimeLeft2.AddDays(1);
TimeSpan result = dtTimeLeft2.Subtract(dtTim
lblTimeLeft.Text = result.Days + " days " + result.Hours + " hours " + result.Minutes + " minutes " + result.Seconds + " seconds";
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
then you parse the Date from the Database
private void DataList1_ItemDataBound(ob
{
DateTime regDate = DateTime.Parse(((DataRowVi
DateTime nowDate = DateTime.Now;
TimeSpan tp = nowDate.SubStract(regDate)
Label lblRegDate = (Label)e.Item.FindControl(
lblRegDate.Text = tp.TotalDays() / 365;
}