Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# subtract time (hours minutes)

Tags:

c#

datetime

Hello Everyone I have some interesting situation.

I want to count how many hours (in minutes) is from 20:00 to 01:00 AM, but i Don't know how, because what i have done is:

pabaigosLaikoLaukelis = 01:00;
pradziosLaikoLaukelis = 20:00;
TimeSpan dt = Convert.ToDateTime(pabaigosLaikoLaukelis)- Convert.ToDateTime(pradziosLaikoLaukelis);
int minutes = (int)dt.TotalMinutes;

And i get result -> -1140 minutes, but I need that answer to be just 5 hours from 20:00 to 01:00.

I know that it is quite easy, but i have no idea how to do it.

like image 808
Audrius Gr Avatar asked Dec 11 '22 16:12

Audrius Gr


1 Answers

you could do something like this

        //Datetime(Year,month,day,hour,min,sec)
        DateTime date1 = new DateTime(2012, 1, 1, 20, 0, 0);
        DateTime date2 = new DateTime(2012, 1, 2, 1, 0, 0);
        string minutes = (date2.Subtract(date1).TotalMinutes).ToString();

Tested and works 300 minutes (5 hours)

like image 123
Thomas Andreè Wang Avatar answered Dec 14 '22 06:12

Thomas Andreè Wang