Improve Your Technology

Just another blog for techology

DateDiff and DateAdd in C#

When I started working on C# date functions I felt deficult. As I am a VB developer never worried about these type of small things.  I don’t know why in C# these functionality not provided. So I prepared myself a class file which can handle basic date functions that exists in VB. Below I provided code for this. I think it can useful for some of the people.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System;
using System.Collections.Generic;
using System.Text;

namespace CustomDate
{
    public enum DateInterval
    {
        Day,
        DayOfYear,
        Hour,
        Minute,
        Month,
        Quarter,
        Second,
        Weekday,
        WeekOfYear,
        Year
    }

    public class DateAndTime
    {
        public static long DateDiff(DateInterval interval, DateTime dt1, DateTime dt2)
        {
            return DateDiff(interval, dt1, dt2, System.Globalization.DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
        }

        private static int GetQuarter(int nMonth)
        {
            if (nMonth <= 3)
                return 1;
            if (nMonth <= 6)
                return 2;
            if (nMonth <= 9)
                return 3;
            return 4;
        }

        public static DateTime DateAdd(DateInterval interval, DateTime dt, Int32 val)
        {
            if (interval == DateInterval.Year)
                return dt.AddYears(val);
            else if (interval == DateInterval.Month)
                return dt.AddMonths(val);
            else if (interval == DateInterval.Day)
                return dt.AddDays(val);
            else if (interval == DateInterval.Hour)
                return dt.AddHours(val);
            else if (interval == DateInterval.Minute)
                return dt.AddMinutes(val);
            else if (interval == DateInterval.Second)
                return dt.AddSeconds(val);
            else if (interval == DateInterval.Quarter)
                return dt.AddMonths(val * 3);
            else
                return dt;
        }

        public static long DateDiff(DateInterval interval, DateTime dt1, DateTime dt2, DayOfWeek eFirstDayOfWeek)
        {
            if (interval == DateInterval.Year)
                return dt2.Year – dt1.Year;

            if (interval == DateInterval.Month)
                return (dt2.Month – dt1.Month) + (12 * (dt2.Year – dt1.Year));

            TimeSpan ts = dt2 – dt1;

            if (interval == DateInterval.Day || interval == DateInterval.DayOfYear)
                return Round(ts.TotalDays);

            if (interval == DateInterval.Hour)
                return Round(ts.TotalHours);

            if (interval == DateInterval.Minute)
                return Round(ts.TotalMinutes);

            if (interval == DateInterval.Second)
                return Round(ts.TotalSeconds);

            if (interval == DateInterval.Weekday)
            {
                return Round(ts.TotalDays / 7.0);
            }

            if (interval == DateInterval.WeekOfYear)
            {
                while (dt2.DayOfWeek != eFirstDayOfWeek)
                    dt2 = dt2.AddDays(-1);
                while (dt1.DayOfWeek != eFirstDayOfWeek)
                    dt1 = dt1.AddDays(-1);
                ts = dt2 – dt1;
                return Round(ts.TotalDays / 7.0);
            }

            if (interval == DateInterval.Quarter)
            {
                double d1Quarter = GetQuarter(dt1.Month);
                double d2Quarter = GetQuarter(dt2.Month);
                double d1 = d2Quarter – d1Quarter;
                double d2 = (4 * (dt2.Year – dt1.Year));
                return Round(d1 + d2);
            }

            return 0;

        }

        private static long Round(double dVal)
        {
            if (dVal >= 0)
                return (long)Math.Floor(dVal);
            return (long)Math.Ceiling(dVal);
        }
    }
}

Let me know if you need any date functions or enhancements in existing functions.

About these ads

September 9, 2009 - Posted by | C#, DateDiff and DateAdd in C#, Technology, Uncategorized |

4 Comments »

  1. Thanks a lot. It really helped.

    Comment by Rakesh | December 14, 2009 | Reply

  2. There some requirements when the period required should include the actual number of years, months and days between two date. eg age, one can be 31 yrs 8 months and 23 days old
    How can this be calculated

    Comment by Tauya Mhangami | June 14, 2010 | Reply

    • I will give you solution, not right away.

      Thanks for your request.

      Comment by srinutamada | June 15, 2010 | Reply

  3. DateDiff and DateAdd in C# « Improve Your Technology…

    Thank you for submitting this cool story – Trackback from Blue Ray Plus – Latest Technology News…

    Trackback by Blue Ray Plus - Latest Technology News | January 10, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: