Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Work out minutes difference between dates

I have the following code:

DateTime pickerDate = Convert.ToDateTime(pickerWakeupDate.SelectedDate);
string enteredStr = pickerDate.ToShortDateString() + " " + textWakeupTime.Text;
string format = "dd/M/yyyy HH:mm";
DateTime enteredDate = DateTime.ParseExact(enteredStr, format, null);

The problem I am facing is that I would like to workout the difference between the set date and the date and time now. This value will then need to provide me a value of how many minutes there are between the dates.

I tried using:

DateTime todaysDateTime = DateTime.Now;
TimeSpan span = enteredDate.Subtract(todaysDateTime);
int totalMins = span.Minutes;

But this gave me an incorrect value 0 when the value was set 10 minutes ahead.

Can anyone help me solve this

Thanks.

like image 605
Sandeep Bansal Avatar asked Jul 13 '11 12:07

Sandeep Bansal


People also ask

How do I calculate minutes between dates in Excel?

=TEXT(TODAY()-D5,"[mm]") Here, the TODAY()-D5 part of the formula returns the difference between the today and the date in D5 in days. The TEXT Function converts this difference to minutes. Press ENTER and you will see the time difference between today and the date in cell D5 by minute.


1 Answers

i think what you really want is span.TotalMinutes (I cant tell you how many times this has caught me out on the TimeSpan class!)

For reference

TimeSpan.Minutes - "Gets the minutes component of the time interval represented by the current TimeSpan structure."

TimeSpan.TotalMinutes - "Gets the value of the current TimeSpan structure expressed in whole and fractional minutes."

like image 195
Jamiec Avatar answered Oct 22 '22 14:10

Jamiec