DateTimeExtensions.cs 5.61 KB
using System;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;

namespace Infrastructure
{
    public static class DateTimeExtensions
    {
        public static string GetHourly(this DateTime datevalue)
        {
            return datevalue.ToString("yyyy/MM/dd HH");
        }
        public static string GetDaily(this DateTime datevalue)
        {
            return datevalue.ToString("yyyy/MM/dd");
        }
        public static string GetWeekly(this DateTime datevalue)
        {
            GregorianCalendar gc = new GregorianCalendar();
            int weekOfYear = gc.GetWeekOfYear(datevalue, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
            return "W" + weekOfYear.ToString();
        }
        public static string GetMonthly(this DateTime datevalue)
        {
            return datevalue.ToString("yyyy/MM");
        }
        public static string GetEnMonthly(this DateTime datevalue)
        {
            string rtn = datevalue.Year.ToString().Substring(2, 2);
            string month1 = "";
            switch (datevalue.Month.ToString())
            {
                case "1":
                    month1 = "Jan";
                    break;
                case "2":
                    month1 = "Feb";
                    break;
                case "3":
                    month1 = "Mar";
                    break;
                case "4":
                    month1 = "Apr";
                    break;
                case "5":
                    month1 = "May";
                    break;
                case "6":
                    month1 = "June";
                    break;
                case "7":
                    month1 = "July";
                    break;
                case "8":
                    month1 = "Aug";
                    break;
                case "9":
                    month1 = "Sept";
                    break;
                case "10":
                    month1 = "Oct";
                    break;
                case "11":
                    month1 = "Nov";
                    break;
                case "12":
                    month1 = "Dec";
                    break;
            }
            return month1 + "-" + rtn;
        }

        public static string GetWeeklyStart(this DateTime datevalue)
        {
            int dayInWeek = (int)datevalue.DayOfWeek;
            if (dayInWeek == 0) { dayInWeek = 7; }
            return datevalue.AddDays(-1 * (dayInWeek - 1)).GetDaily();
        }

        public static string GetWeeklyEnd(this DateTime datevalue)
        {
            int dayInWeek = (int)datevalue.DayOfWeek;
            if (dayInWeek == 0) { dayInWeek = 7; }
            return datevalue.AddDays(-1 * (dayInWeek - 1)).AddDays(6).GetDaily();
        }

        public static string GetFormat(this DateTime datevalue, string dateType)
        {
            string rtn = datevalue.ToString("yyyy/MM/dd HH:mm:ss");
            switch (dateType.ToLower())
            {
                case "hourly":
                    rtn = datevalue.GetHourly();
                    break;
                case "daily":
                    rtn = datevalue.GetDaily();
                    break;
                case "weekly":
                    rtn = datevalue.GetWeekly();
                    break;
                case "monthly":
                    rtn = datevalue.GetMonthly();
                    break;
                case "weeklystart":
                    rtn = datevalue.GetWeeklyStart();
                    break;
                case "weeklyend":
                    rtn = datevalue.GetWeeklyEnd();
                    break;
                default:
                    break;
            }
            return rtn;
        }

        public static int GetTimeSpanDays(this DateTime datevalue1, DateTime datevalue2)
        {
            int rtn;
            TimeSpan timeSpan = datevalue2.Subtract(datevalue1);
            rtn = timeSpan.Days;
            return rtn;
        }

        public static int GetTimeSpanHours(this DateTime datevalue1, DateTime datevalue2)
        {
            int rtn;
            TimeSpan timeSpan = datevalue2.Subtract(datevalue1);
            rtn = timeSpan.Days * 24 + timeSpan.Hours;
            return rtn;
        }

        public static int GetTimeSpanMinutes(this DateTime datevalue1, DateTime datevalue2)
        {
            int rtn;
            TimeSpan timeSpan = datevalue2.Subtract(datevalue1);
            rtn = (timeSpan.Days * 24 + timeSpan.Hours) * 60 + timeSpan.Minutes;
            return rtn;
        }

        public static int GetTimeSpanSeconds(this DateTime datevalue1, DateTime datevalue2)
        {
            int rtn;
            TimeSpan timeSpan = datevalue2.Subtract(datevalue1);
            rtn = ((timeSpan.Days * 24 + timeSpan.Hours) * 60 + timeSpan.Minutes) * 60 + timeSpan.Seconds;
            return rtn;
        }

        public static int GetTimeSpan(this DateTime datevalue1, DateTime datevalue2, string spanType)
        {
            int rtn = 0;
            switch (spanType.ToLower())
            {
                case "days":
                    rtn = GetTimeSpanDays(datevalue1, datevalue2);
                    break;
                case "hours":
                    rtn = GetTimeSpanHours(datevalue1, datevalue2);
                    break;
                case "minutes":
                    rtn = GetTimeSpanMinutes(datevalue1, datevalue2);
                    break;
                case "seconds":
                    rtn = GetTimeSpanSeconds(datevalue1, datevalue2);
                    break;
                default:
                    break;
            }
            return rtn;
        }
    }
}