开发者

Work out minutes difference between dates

开发者 https://www.devze.com 2023-03-20 12:18 出处:网络
I have the following code: DateTime pickerDate = Convert.ToDateTime(pickerWakeupDate.SelectedDate); string enteredStr = pickerDate.ToShortDateString() + \" \" + textWakeupTime.Text;

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开发者_如何学Python);

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.


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消