C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » ASP.NET »

Difference between two dates


Posted Date: 04 Nov 2008      Posted By: Badal Sharma      Member Level: Silver     Points: 1   Responses: 6



how to get different between two dates using C# ASP.NET 2.0




Responses

Author: Tausif L Nakhuda    04 Nov 2008Member Level: SilverRating: 4 out of 54 out of 54 out of 54 out of 5     Points: 6

Hi Badal,
You can use the substract method of the DateTime class to get the difference between the 2 dates.

e.g.
Considering 2 dateTime variables , the difference can be calculated
as shown in the code below.

DateTime d1 = new DateTime(8,11,5);
DateTime d2 = new DateTime(8,11,4);
MessageBox.Show( d1.Subtract(d2).TotalDays.ToString());


Hope this helps.
Regards.
T.L.N



Author: archana    04 Nov 2008Member Level: GoldRating: 4 out of 54 out of 54 out of 54 out of 5     Points: 6

hi

you can use any of the followiing
(1) DateTime firstdate =Convert.ToDateTime("03/08/2006");
DateTime secondate =Convert.ToDateTime("07/08/2007");

TimeSpan TS = secondate - firstdate;

Response.Write(TS.TotalDays);

(2) Dim d2 As DateTime
d1 = Convert.ToDateTime("03/08/2006")
d2 = DateTime.Now
Dim s As String
s = FormatNumber((DateDiff(DateInterval.Day, d1, d2)))
Response.Write(s)

(3)
select DATEDIFF(dd,Col_Name,getdate()) from table_name



Author: Rajaraman    04 Nov 2008Member Level: DiamondRating: 4 out of 54 out of 54 out of 54 out of 5     Points: 6

DateTime.Subtract method returns a TimeSpan object, which has properties such as Days, Hours, Minutes and so on to get the difference in specific measurements.

Please find the code snippet for more info.

// Start date
DateTime startDate = new DateTime(2005, 2, 1, 3, 4, 12, 56);
// End date
DateTime endDate = new DateTime(2005, 12, 12, 4, 30, 45, 12);
// Time span
TimeSpan diffDate = endDate.Subtract ( startDate );
// Spit it out
Console.WriteLine( "Time Difference: ");
Console.WriteLine(diffDate.Days.ToString() + " Days");
Console.WriteLine(diffDate.Hours.ToString() + " Hours" );
Console.WriteLine(diffDate.Minutes.ToString() + " Minutes");
Console.WriteLine(diffDate.Seconds.ToString() + " Seconds ");
Console.WriteLine(diffDate.Milliseconds.ToString() + " Milliseconds ");


Let me know if you have any questions.



Author: Anil Kumar Pandey    04 Nov 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

hi,

u can use the inbuilt datetime function for doing the task u can use the following method to get the date difference


DateTime dt;
DateTime dt2;
dt.Subtract(dt2);


Regards
Anil Pandey

Thanks & Regards
Anil Kumar Pandey



Author: Gaurav Agrawal    04 Nov 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

DateTime dt1 = new DateTime();
DateTime dt2 = new DateTime();
TimeSpan ts = dt2.Subtract(dt1);

GA

Thanks & Regards,

Gaurav Agrawal
Sr.Software Engineer
gaur1982@yahoo.com
09829373514



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : sending emails from outlook in asp.net using c#
Previous : favicon.ico An icon before address bar
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use