Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
<%#System.DateTime.Now.Subtract(Eval("Date"))%>

This is not a correct format but i want to know how to declare it.
I want subtract date from current date in aspx source code.
Please help me...Thanks in advance
Posted
Comments
Mohd Wasif 21-May-11 3:05am    
Here Please let me know that you want difference between two dates in form of day of date

DateTime.Now.Subtract method expects a DateTime value. You code shall work if you just convert the evaluated statement to DateTime.

Change your code to the one below:
XML
<%#System.DateTime.Now.Subtract(Convert.ToDateTime(Eval("Date")))%> 

and it should work.

Hope this helps!
 
Share this answer
 
v2
Comments
rahul dev123 21-May-11 3:50am    
it will shown error like this:
Invalid cast from 'Int32' to 'DateTime'.
Ankur\m/ 21-May-11 5:10am    
It means that your "Date" column returns an integer value from the database.
Check what value does your database return.
Here if you are fetching data from a table and then binding in Data Control then you can simply write query as


select datediff(d,getdate(),date)as date from tablename

if you wanr it in days

other option
in aspx.cs page
is

DateTime t1 = DateTime.Now;
DateTime t2 = seconddate;
int i = DateTime.Compare(t1, t2);
 
Share this answer
 
v3
Comments
rahul dev123 21-May-11 3:16am    
I have also need more columns data from database so i can't write this select command. I also need Id, Name,address from this table so how can i do that. Please help me
Mohd Wasif 21-May-11 3:25am    
so what is the problem use query as per your selection of column of table
Mohd Wasif 21-May-11 3:26am    
select datediff(d,getdate(),date)as date,Id, Name,address from tablename
rahul dev123 21-May-11 3:27am    
Here also one problem.If comment is post today then it will display 0 days ago. I want if comment post today then show how many hours ago.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900