Work out minutes difference between dates
Asked Answered
H

2

17

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.

Hydroponics answered 13/7, 2011 at 12:19 Comment(1)
I would verify that the enteredDate is what you think it is. The second block of code should work in this instance. You still want to use TotalMinutes since something like 1hr 1min will return "1" for minutes which is almost surely not what you want, but that wouldn't be the problem that you've laid out here.Aster
L
28

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."

Lobster answered 13/7, 2011 at 12:22 Comment(4)
Total minutes was the one! Thanks for that, such a simple mistake.Hydroponics
@Sandeep - Believe me youre not alone. I made the same mistake as recently as this week!Lobster
@Sandeep Interesting, since both TotalMinutes and Minutes should return 10 and not 0 for the problem you described. (a TimeSpan of 10 minutes)Aster
@kekekela I have no idea why either, there may have been another error somewhere, but it's all fixed nowHydroponics
R
0

I Try an extension it resolve more than minutes you could improve it:

public enum eTimeFragment
    {
        hours,
        minutes,
        seconds,
        milliseconds
    }


public static long DiferenceIn(this DateTime dtOrg, DateTime Diff, eTimeFragment etf = eTimeFragment.minutes)
        {

            long InTicks = 1;
            switch (etf)
            {
                case eTimeFragment.hours:
                    InTicks = DateTime.MinValue.AddHours(1).Ticks;
                    break;
                case eTimeFragment.seconds:
                    InTicks = DateTime.MinValue.AddSeconds(1).Ticks;
                    break;
                case eTimeFragment.milliseconds:
                    InTicks = DateTime.MinValue.AddMilliseconds(1).Ticks;
                    break;
                case eTimeFragment.minutes:
                default:
                    InTicks = DateTime.MinValue.AddMinutes(1).Ticks;
                    break;
            }

            if (dtOrg > Diff)
                return dtOrg.AddTicks(Diff.Ticks * -1).Ticks / InTicks;
            else
                return Diff.AddTicks(dtOrg.Ticks * -1).Ticks / InTicks;

        }

Using it, example in debug console:

 DateTime fromDate = DateTime.Now;
    //Default is Minutes fragment
    fromDate.DiferenceIn(fromDate.AddHours(4))
    240
    fromDate.DiferenceIn(fromDate.AddHours(50))
    3000
    fromDate.DiferenceIn(fromDate.AddDays(1))
    1440
    fromDate.DiferenceIn(fromDate.AddDays(1),WGTS_Extensions.eTimeFragment.hours)
    24
    fromDate.DiferenceIn(fromDate.AddDays(1),WGTS_Extensions.eTimeFragment.seconds)
    86400
    fromDate.DiferenceIn(fromDate.AddHours(1),WGTS_Extensions.eTimeFragment.seconds)
    3600
Rourke answered 29/8, 2016 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.