Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All
i want to write a method;
1-get start time
2-get end time
3-calculate time between them
also for date:
1-get start date
2-get end date
3-calculate date between them

please help,thanks everyone
Posted
Comments
Andreas Gieriet 15-Aug-12 6:46am    
What do you mean by "get ..."? Do you mean: a dialog that asks the user to enter a time? Or do you mean that the function has two arguments (start and end time) and returns the delta time?
Cheers
Andi

Modify as required.

C#
// Times
            DateTime time1 = DateTime.Parse("22:00");
            DateTime time2 = DateTime.Parse("23:45");
            TimeSpan timeDiff = time2 - time1;
            Console.WriteLine("There are " + timeDiff.Minutes + " minutes between the two times.");

            // Dates
            DateTime date1 = DateTime.Parse("17 March 2012");
            DateTime date2 = DateTime.Parse("14 June 2012");
            TimeSpan dateDiff = date2 - date1;
            Console.WriteLine("There are " + dateDiff.Days + " days between the two dates.");
            Console.ReadLine();
 
Share this answer
 
Comments
FM7 15-Aug-12 6:50am    
thanks very much
See TimeSpan Class[^] for delta times and DateTime Class[^] for absolute times.

Cheers
Andi
 
Share this answer
 
Comments
FM7 15-Aug-12 6:50am    
thanks very much
Please check the below example.

C#
static void Main(string[] args)
       {
           DateTime tm1 = DateTime.Parse("05:35");
           DateTime tm2 = DateTime.Parse("13:57");
           TimeSpan ts = (tm2 - tm1);
           Console.WriteLine(ts);

           DateTime dt1= DateTime.Parse("5/12/2012");
           DateTime dt2 = DateTime.Parse("7/12/2012");
           int days = (dt2 - dt1).Days;
           Console.WriteLine(days);
           Console.Read();
       }


This will return u
08:22(time diff)
2(date diff)
 
Share this answer
 
Comments
FM7 15-Aug-12 6:52am    
thanks very much.OK

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