ASP.NET / C#: Date Comparison Function Crashes

This works for the first minute, then it crashes with this error:
Crashed in OnTick with FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RoboRobot : Robot
    {
        public DateTime StartDateTime;


        protected override void OnStart()
        {
            StartDateTime = DateTime.Now;
        }

        protected override void OnTick()
        {
            Print(GetTimeSpan(StartDateTime));
        }


        public static string GetTimeSpan(DateTime postDate)
        {
            string stringy = string.Empty;
            TimeSpan diff = DateTime.Now.Subtract(postDate);
            double days = diff.Days;
            double hours = diff.Hours + days * 24;
            double minutes = diff.Minutes + hours * 60;
            if (minutes <= 1)
            {
                return "Just Now";
            }
            double years = Math.Floor(diff.TotalDays / 365);
            if (years >= 1)
            {
                return string.Format("{0} year{1} ago", years, years >= 2 ? "s" : null);
            }
            double weeks = Math.Floor(diff.TotalDays / 7);
            if (weeks >= 1)
            {
                double partOfWeek = days - weeks * 7;
                if (partOfWeek > 0)
                {
                    stringy = string.Format(", {0} day{1}", partOfWeek, partOfWeek > 1 ? "s" : null);
                }
                return string.Format("{0} week{1}{2} ago", weeks, weeks >= 2 ? "s" : null, stringy);
            }
            if (days >= 1)
            {
                double partOfDay = hours - days * 24;
                if (partOfDay > 0)
                {
                    stringy = string.Format(", {0} hour{1}", partOfDay, partOfDay > 1 ? "s" : null);
                }
                return string.Format("{0} day{1}{2} ago", days, days >= 2 ? "s" : null, stringy);
            }
            if (hours >= 1)
            {
                double partOfHour = minutes - hours * 60;
                if (partOfHour > 0)
                {
                    stringy = string.Format(", {0} minute{1}", partOfHour, partOfHour > 1 ? "s" : null);
                }
                return string.Format("{0} hour{1}{2} ago", hours, hours >= 2 ? "s" : null, stringy);
            }
            return minutes.ToString("{0} minutes ago");
        }

    }
}

Open in new window

LVL 10
skijAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
Replace line 39
return string.Format("{0} year{1} ago", years, years >= 2 ? "s" : null);

Open in new window

with
return string.Format("{0} year{1} ago", years, years >= 2 ? "s" : String.Empty);

Open in new window


For format to work you need valid string on your second variable. If null is returned, format assumes there is only variable to format and throws the error you described.
0
anarki_jimbelSenior DeveloperCommented:
I believe, the error happens in the very last statement:

            return minutes.ToString("{0} minutes ago");

Open in new window


You have a placeholder for the parameter but do not pass a parameter itself.

So, I believe it should be

minutes
            return string.Format("{0} minutes ago", minutes);

Open in new window


"minutes" is declared at the beginning of the GetTimeSpan() method.

And yes - use string.Format method to get a message.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.