TimeSpan countdown between given dates

1.6k Views Asked by At

I would to calculate between two given dates the TimeSpan, counting down to 0 days. I have the following code:

partial void mExpiration_Compute(ref string result)
    {

        DateTime a = mExpiryDate.AddMonths(-1);
        DateTime b = mExpiryDate;

        TimeSpan ts = b.Subtract(a);

        // Difference in days.
        int c = ts.Days;

        result = Convert.ToString(c);

    }

If the TimeSpan is currently 31, then I would like the reducing balance time. Example: "You have 24 days to expiry date." Note: The balance reducing is ( 31, 30, .., 24 etc)

How do I count down between given dates.

5

There are 5 best solutions below

2
Brendan Green On

Your function never returns anything other 31, because you are calculating the days remaining exactly one month relative to the expiry date (i.e. the function always calculates the days between 02/09/215 and 02/08/2015, thus always returns a constant value).

Assuming your mExpiryDate is persisted and does not change, this is how I would calculate the days remaining (relative to the current date):

TimeSpan ts = mExpiryDate.Subtract(DateTime.Now);
return ts.Days;
2
lastmannorth On

I think you're missing a parameter. Is this what you need:

static DateTime mExpiryDate = DateTime.Parse("09/29/2015");
public static void Main()
{
    var result = string.Empty;
    var noticeDate = DateTime.Parse("09/05/2015");
    mExpiration_Compute(ref result, noticeDate);
    Console.WriteLine(result);
}

public static void mExpiration_Compute(ref string result, DateTime noticeDate)
{
    DateTime a = noticeDate;
    DateTime b = mExpiryDate;
    TimeSpan ts = b.Subtract(a);
    result = string.Format("You have {0} days to expiry date.", ts.Days);
}

https://dotnetfiddle.net/9mDPx2

0
Gustav On

Without knowing the logic of your form, I guess the question is wether expiry date exists or not.

If it exists, retrieve it; if not, create it as of today and add one month.

Then calculate the days to expiration.

It would go like this (stand-alone code, adjust to your application):

// Choose either setting 1. or 2. for mExpiryDate.
// Here setting 1. is active:

//      1. Simulate expiry date already set on August 17th.
DateTime mExpiryDate = new DateTime(2015, 9, 17);

//      2. Expiry date not set.
//DateTime mExpiryDate = DateTime.Today.AddMonths(1);

TimeSpan ts = mExpiryDate.Subtract(DateTime.Today);

// Difference in days.
int c = ts.Days;

string result = Convert.ToString(c);

This will on this day, 2015-09-01, return 16.

0
Xdrone On

This the solution for the countdown between given dates.

    partial void mExpiration_Compute(ref string result)
    {

        DateTime expStart = mExpiryDate.AddMonths(-1);
        DateTime expDate = mExpiryDate;
        DateTime currentDate = DateTime.Now;

        // Difference in days, hours, and minutes.
        TimeSpan expSpanGivenDates = expDate.Subtract(expStart);
        TimeSpan expSpanDateNow = expDate.Subtract(currentDate);

        // Difference in days
        int numGivenDays = expSpanGivenDates.Days;
        int numNowDays = expSpanDateNow.Days;
        int calculateDays = numGivenDays - (numGivenDays - numNowDays);

        int days = 0;

        if (calculateDays >= 0 && calculateDays < 31)
        {
            days = calculateDays;
        }

        result = Convert.ToString(days);

    }

It will return the number of days left between two given dates. It will be 0 before and after expiry period.

The output would be: 0 0 31 24 8 9 0 0 0 .....

2
Dr.Marcelo On

This works good!

    public TimeSpan ElapsedTimeFormatted
    {
        get
        {
            if (FinishedOn != null &&
                StartedAt != null)
            {
                TimeSpan durationCount = new TimeSpan();

                int hours = 0;
                int minutes = 0;
                int seconds = 0;

                var times = Segments.Select(c => c.ElapsedTimeFormatted).ToList();

                foreach (var time in times)
                {
                    TimeSpan timeParse = TimeSpan.Parse(time);

                    hours = hours + (int)timeParse.Hours;
                    minutes = minutes + (int)timeParse.Minutes;
                    seconds = seconds + (int)timeParse.Seconds;

                    durationCount = new TimeSpan(hours, minutes, seconds);
                }

                return durationCount;
            }

            return new TimeSpan();
        }
    }