-3

I've got interview task to do. I need to calculate number of dates between to dates WITHOUT using Date/DateTime/TimeSpan classes to do that.

How to do that by using the String class only?

Best Regards //vamone

vamone
  • 303
  • 1
  • 2
  • 10
  • `string` only? You cannot even use `int`? Also if you show us an attempt you're more likely to get help. – juharr Jan 05 '17 at 15:11
  • I'm sorry I was unclean. I have date as a string in format "1/5/2017" for example. – vamone Jan 05 '17 at 15:12
  • Does the interviewer wants to know if you are able to do nonsensical task without revolting? – Ralf Jan 05 '17 at 15:16
  • @vamone Perhaps you have misunderstood your interviewers? Or perhaps they have a bit simplistic view of all the issues one has to consider while implementing proper custom datetime library (http://infiniteundo.com/post/25326999628/falsehoods-programmers-believe-about-time, http://www.dadhacker.com/blog/?p=1585). – Eugene Podskal Jan 05 '17 at 15:17

2 Answers2

2

It's more normal to convert your date into a Julian day number:

// This is a standard formula for conversion.
// See the Wikipedia page on Julian days for more information.

public static long ToJulian(int year, int month, int day)
{
    if (month < 3)
    {
        month = month + 12;
        year = year - 1;
    }

    return  day + (153 * month - 457) / 5 + 365 * year + (year / 4) - (year / 100) + (year / 400) + 1721119;
}

To call that you need to first parse your date string into month, day and year:

    public static long ToJulian(string mdy)
    {
        var split = mdy.Split('/');
        return ToJulian(int.Parse(split[2]), int.Parse(split[0]), int.Parse(split[1]));
    }

Then you can convert the two dates into Julian format and subtract them to find the difference in days.

Here's an example which shows that the result is the same via Julian compared to using DateTime and TimeSpan:

    static void Main()
    {
        string date1 = "5/31/1961";
        string date2 = "1/5/2017";

        long diff1 = ToJulian(date2) - ToJulian(date1);

        Console.WriteLine("diff1 = " + diff1);

        long diff2 = (long)(
            DateTime.Parse(date2, CultureInfo.InvariantCulture) - 
            DateTime.Parse(date1, CultureInfo.InvariantCulture))
            .TotalDays;

        Console.WriteLine("diff2 = " + diff2);
    }
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
1

You can't without knowing which Culture the date is tied to.

You must first know which Culture we are talking about, then you can identify separators and date parts with the DateFormat object.

Then just use the Split method passing date separators in, so you can retrieve each Date part and perform your computations.

But you also have to define months duration (28 days, 30 days, 31 days), not to mention leap years.

To determine leap-years you can implement the following algorithm

if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)

For months duration declare an Array of twelve items to store months duration and use the month number as an index to retrieve the length of each month.