Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a project where one datetime field is there.

If user posted a comment then it show the time and date of the comment.
But i want the code where it compares the comment date to todays date and view the result like 1 days ago, 2 days ago etc.

Please help me.

Thanks in Advance
Posted
Updated 20-May-11 21:29pm
v2

Firstly, you will be having a date of comment posted right? Ex: PostedDate

secondly, you can fetch today's date. using DateTime.Now. Ex: CurrentDate

Finally, Compare it using

TimeSpan t = CurrentDate.Substract(PostedDate);


Now, say

t.Days to get difference :)

Thanks
 
Share this answer
 
v2
Comments
rahul dev123 21-May-11 2:21am    
Thanks for your solution but i want to use in datalist in source code here how can i do that? I retrieve data from database using datalist and view those data using Eval function. So how can i do that in source code.
this is quite simple:


just write query as:

select datediff(d,getdate(),date)

as date from tablename

and bind in Datalist:

<%# DataBinder.Eval(Container.DataItem, "date")%>
 
Share this answer
 
v2
Comments
rahul dev123 21-May-11 3:54am    
If the comment is posted 1 hour ago then it show 0 days ago.I want to show 1 hour ago
Mohd Wasif 21-May-11 4:11am    
select datediff(hh,getdate(),date)
then use it you will get

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