C#中日期格式数据的一些处理方法(转)

来源:互联网 发布:魔兽世界mac怎么玩 编辑:程序博客网 时间:2024/05/16 22:50

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

namespace SystemFramework.CommonFunctions
{
    /// <summary>
    /// Summary description for DateLib.
    /// </summary>
    public class DateTimeManage
    {
        public DateTimeManage()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        /// <summary>
        /// 比较给定日期和当前日期的大小
        /// </summary>
        /// <param name="strDate">给定日期</param>
        /// <returns>给定日期大:>0 相等:0 当前日期大: <0</returns>
        public static int CompareDate(string strDate)
        {
            strDate = strDate.Replace("-", "");
            DateTime dt = DateTime.Now;
            string strNow = dt.ToString("yyyy-MM-dd");
            return CompareDate(strDate, strNow);
        }
        /// <summary>
        /// 比较两个日期的大小

        /// </summary>
        /// <param name="strDate">第一个日期</param>
        /// <param name="strDate2">第二个日期</param>
        /// <returns>第一个大:>0 相等:0 第二个大: <0</returns>
        public static int CompareDate(string strDate, string strDate2)
        {
            strDate = strDate.Replace("-", "");
            strDate2 = strDate2.Replace("-", "");
            return strDate.CompareTo(strDate2);
        }
        /// <summary>
        /// 是否是日期型
        /// 支持的格式包括("1980-06-06"和"1980/06/06")
        /// </summary>
        /// <param name="dateStr"></param>
        /// <returns></returns>
        public static bool IsDate(string dateStr)
        {

            string datePat = @"^(/d{4})(//|-)(/d{1,2})(//|-)(/d{1,2})$";
            bool IsMatch = Regex.IsMatch(dateStr, datePat);
            //日期格式不正确,正确格式:1980-06-06
            if (!IsMatch)
                return false;
            string[] dates = dateStr.Split('/', '-');
            if (dates == null || dates.Length < 3)
                return false;

            int year = int.Parse(dates[0]);
            int month = int.Parse(dates[1]);
            int day = int.Parse(dates[2]);

            if (month < 1 || month > 12)
            {
                //"月份必须是1月-12月之间";
                return false;
            }

            if (day < 1 || day > 31)
            {
                //"日必须是1-31日之间";
                return false;
            }

            if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31)
            {
                //month doesn't have 31 days!"
                return false;
            }

            if (month == 2)
            { // check for february 29th
                bool isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
                if (day > 29 || (day == 29 && !isleap))
                {
                    // year + " 年 2 月没有 " + day + " 日"
                    return false;
                }
            }
            return true; // date is valid
        }
        /// <summary>
        /// 将星期几数字转换为中文
        /// </summary>
        /// <param name="num">星期几:周日用0表示</param>
        /// <returns></returns>
        public static string SwitchToChinNum(int num)
        {
            if (num == 0) return "日";
            if (num == 1) return "一";
            if (num == 2) return "二";
            if (num == 3) return "三";
            if (num == 4) return "四";
            if (num == 5) return "五";
            if (num == 6) return "六";

            return "";
        }
        /// <summary>
        /// 得到两日期天数差值
        /// </summary>
        /// <param name="sDate">开始日期,格式:"yyyy-MM-dd"</param>
        /// <param name="eDate">结束日期</param>
        /// <returns></returns>
        public static int GetDateDiff(string sDate, string eDate)
        {
            DateTime startDate, endDate;

            try
            {
                startDate = DateTime.Parse(sDate);
                endDate = DateTime.Parse(eDate);

                int days = GetDateDiff(startDate, endDate);
                return days;
            }
            catch (System.Exception exp)
            {
                throw exp;
            }
        }
        /// <summary>
        /// 得到两日期天数差值
        /// </summary>
        /// <param name="sDate">开始日期</param>
        /// <param name="eDate">结束日期</param>
        /// <returns></returns>
        public static int GetDateDiff(System.DateTime sDate, System.DateTime eDate)
        {
            string days = "0";
            days = Convert.ToString(eDate.Subtract(sDate).Days);
            return int.Parse(days);
        }

        /// <summary>
        /// 返回“yyyy-MM-dd hh:mm:ss”格式的字符串
        /// </summary>
        /// <param name="Dt">DateTime变量</param>
        /// <returns>“yyyy-MM-dd hh:mm:ss”格式的字符串</returns>
        public static string ToSTDDateTimeStr(DateTime Dt)
        {
            return Dt.ToString("yyyy-MM-dd hh:mm:ss");
        }

        /// <summary>
        /// 返回“yyyy-MM-dd hh:mm:ss”格式的字符串
        /// </summary>
        /// <param name="Dt">含有日期和时间的字符串</param>
        /// <returns>“yyyy-MM-dd hh:mm:ss”格式的字符串</returns>
        public static string ToSTDDateTimeStr(string Dt)
        {
            if (StrManage.IsDate(Dt))
            {
                return DateTime.Parse(Dt).ToString("yyyy-MM-dd hh:mm:ss");
            }
            else
            {
                throw new Exception("日期格式不正确!");
            }
        }

        /// <summary>
        /// 返回“yyyy-mm-dd”格式的字符串
        /// </summary>
        /// <param name="Dt">DateTime变量</param>
        /// <returns>“yyyy-mm-dd”格式的字符串</returns>
        public static string ToSTDDateStr(DateTime Dt)
        {
            return Dt.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 返回“yyyy-mm-dd”格式的字符串
        /// </summary>
        /// <param name="Dt">含有日期的字符串</param>
        /// <returns>“yyyy-mm-dd”格式的字符串</returns>
        public static string ToSTDDateStr(string Dt)
        {
            if (StrManage.IsDate(Dt))
            {
                return DateTime.Parse(Dt).ToString("yyyy-MM-dd");
            }
            else
            {
                throw new Exception("日期格式不正确!");
            }
        }

        /// <summary>
        /// 返回“hh:mm:ss”格式的字符串
        /// </summary>
        /// <param name="Dt">DateTime变量</param>
        /// <returns>“hh:mm:ss”格式的字符串</returns>
        public static string ToSTDTimeStr(DateTime Dt)
        {
            return Dt.ToString("hh:mm:ss");
        }

        /// <summary>
        /// 返回“hh:mm:ss”格式的字符串
        /// </summary>
        /// <param name="Dt">含有时间的字符串</param>
        /// <returns>“hh:mm:ss”格式的字符串</returns>
        public static string ToSTDTimeStr(string Dt)
        {
            if (StrManage.IsDate(Dt))
            {
                return DateTime.Parse(Dt).ToString("hh:mm:ss");
            }
            else
            {
                throw new Exception("时间格式不正确!");
            }
        }

        /// <summary>
        /// 返回ToDate减去FromDate后的时间单位数量,根据DatePart参数可能是年、月、日、时、分、秒
        /// </summary>
        /// <param name="FromDate">减数时间</param>
        /// <param name="ToDate">被减数时间</param>
        /// <param name="DatePart">差的单位:yy,mm,dd, hh, mi, ss</param>
        /// <returns>ToDate减去FromDate后的时间单位数量,根据DatePart参数可能是年、月、日、时、分、秒</returns>
        public static double DateDiff(DateTime FromDate, DateTime ToDate, string DatePart)
        {
            TimeSpan ts = new TimeSpan();
            double Year = 0;
            double Month = 0;
            ts = ToDate - FromDate;
            switch (DatePart)
            {
                case "ss":
                    {
                        return ts.TotalSeconds;
                    }
                case "mi":
                    {
                        return ts.TotalMinutes;
                    }
                case "hh":
                    {
                        return ts.TotalHours;
                    }
                case "dd":
                    {
                        return ts.TotalDays;
                    }
                case "mm":
                    {
                        Year = ToDate.Year - FromDate.Year;
                        Month = ToDate.Month - FromDate.Month;
                        return Year * 12 + Month;
                    }
                case "yy":
                    {
                        Year = ToDate.Year - FromDate.Year;
                        return Year;
                    }
                default:
                    {
                        throw new Exception("请输入正确的时间单位字符串!");
                    }
            }
        }

        /// <summary>
        /// 给Dt中加上时间,使之变成yyyy-mm-dd 00:00:00形式
        /// </summary>
        /// <param name="Dt">DateTime变量</param>
        /// <returns>yyyy-mm-dd 00:00:00形式的DateTime值</returns>
        public static DateTime ToStartDateTime(DateTime Dt)
        {
            return new DateTime(Dt.Year, Dt.Month, Dt.Day, 0, 0, 0);
        }

        /// <summary>
        /// 给Dt中加上时间,使之变成yyyy-mm-dd 00:00:00形式
        /// </summary>
        /// <param name="Dt">含有日期的字符串</param>
        /// <returns>yyyy-mm-dd 00:00:00形式的DateTime值</returns>
        public static DateTime ToStartDateTime(string Dt)
        {
            if (Dt != "")
            {
                if (StrManage.IsDate(Dt))
                {
                    DateTime DtRtn = DateTime.Parse(Dt);
                    return new DateTime(DtRtn.Year, DtRtn.Month, DtRtn.Day, 0, 0, 0);
                }
                else
                {
                    throw new Exception("日期格式不正确!");
                }
            }
            else
            {
                return new DateTime(1900, 1, 1, 0, 0, 0);
            }
        }

        /// <summary>
        /// 给Dt中加上时间,使之变成yyyy-mm-dd 23:59:59形式
        /// </summary>
        /// <param name="Dt">DateTime变量</param>
        /// <returns>yyyy-mm-dd 23:59:59形式的DateTime值</returns>
        public static DateTime ToEndDateTime(DateTime Dt)
        {
            return new DateTime(Dt.Year, Dt.Month, Dt.Day, 23, 59, 59);
        }

        /// <summary>
        /// 给Dt中加上时间,使之变成yyyy-mm-dd 23:59:59形式
        /// </summary>
        /// <param name="Dt">含有日期的字符串</param>
        /// <returns>yyyy-mm-dd 23:59:59形式的DateTime值</returns>
        public static DateTime ToEndDateTime(string Dt)
        {
            if (Dt != "")
            {
                if (StrManage.IsDate(Dt))
                {
                    DateTime DtRtn = DateTime.Parse(Dt);
                    return new DateTime(DtRtn.Year, DtRtn.Month, DtRtn.Day, 23, 59, 59);
                }
                else
                {
                    throw new Exception("日期格式不正确!");
                }
            }
            else
            {
                return new DateTime(2100, 1, 1, 23, 59, 59);
            }
        }
    }

原创粉丝点击