C# 时间操作类

public class TimeOperate
    {
        /// <summary>
        /// 获取周第一天
        /// </summary>
        /// <param name="argDatetime"></param>
        /// <returns></returns>
        public static DateTime GetFirstDayOfWeek(DateTime argDatetime)
        {
            return argDatetime.AddDays(1 - Convert.ToInt32(argDatetime.DayOfWeek.ToString("d")));   //本 周周一
        }

        /// <summary>
        /// 获取周最后一天
        /// </summary>
        /// <param name="argDatetime"></param>
        /// <returns></returns>
        public static DateTime GetLastDayOfWeek(DateTime argDatetime)
        {
            return GetFirstDayOfWeek(argDatetime).AddDays(6);
        }

        /// <summary>
        /// 取得某月的第一天
        /// </summary>
        /// <param name="argDatetime">本月份某一天</param>
        /// <returns>某月的第一天</returns>
        public static DateTime GetFirstDayOfMonth(DateTime argDatetime)
        {
            return argDatetime.AddDays(1 - argDatetime.Day);
        }

        /// <summary>
        /// 取得某月的最后一天
        /// </summary>
        /// <param name="argDatetime">本月份某一天</param>
        /// <returns>某月的最后一天</returns>
        public static DateTime GetLastDayOfMonth(DateTime argDatetime)
        {
            return GetFirstDayOfMonth(argDatetime).AddMonths(1).AddDays(-1);
        }

        /// <summary>
        /// 取得某季度的第一天
        /// </summary>
        /// <param name="argDatetime">某一天</param>
        /// <returns>季度的第一天</returns>
        public static DateTime GetFirstDayOfQuarter(DateTime argDatetime)
        {
            int m = 0;
            switch (argDatetime.Month)
            {
                case 1:
                case 2:
                case 3:
                    m = 1; break;
                case 4:
                case 5:
                case 6:
                    m = 4; break;
                case 7:
                case 8:
                case 9:
                    m = 7; break;
                case 10:
                case 11:
                case 12:
                    m = 11; break;
            }

            return new DateTime(argDatetime.Year, m, 1);
        }

        /// <summary>
        /// 取得某季度的最后一天
        /// </summary>
        /// <param name="argDatetime">某一天</param>
        /// <returns>季度的最后一天</returns>
        public static DateTime GetLastDayOfQuarter(DateTime argDatetime)
        {
            int m = 0;
            switch (argDatetime.Month)
            {
                case 1:
                case 2:
                case 3:
                    m = 1; break;
                case 4:
                case 5:
                case 6:
                    m = 4; break;
                case 7:
                case 8:
                case 9:
                    m = 7; break;
                case 10:
                case 11:
                case 12:
                    m = 11; break;
            }

            return new DateTime(argDatetime.Year, m, 1).AddMonths(3).AddDays(-1);
        }

        /// <summary>
        /// 获取年第一天
        /// </summary>
        /// <param name="argDatetime"></param>
        /// <returns></returns>
        public static DateTime GetFirstDayOfYear(DateTime argDatetime)
        {
            return new DateTime(argDatetime.Year, 1, 1); 
        }

        /// <summary>
        /// 获取年最后一天
        /// </summary>
        /// <param name="argDatetime"></param>
        /// <returns></returns>
        public static DateTime GetLastDayOfYear(DateTime argDatetime)
        {
            return new DateTime(argDatetime.Year,  12 ,  31 );
        }

        /// <summary>
        /// 根据时间获取星期几
        /// </summary>
        /// <param name="argDatetime">时间</param>
        /// <returns>星期几</returns>
        public static string GetWeekByTime(DateTime argDatetime)
        {
            string week = argDatetime.DayOfWeek.ToString();

            switch (week)
            {
                case "Monday": week = "星期一"; break;
                case "Tuesday": week = "星期二"; break;
                case "Wednesday": week = "星期三"; break;
                case "Thursday": week = "星期四"; break;
                case "Friday": week = "星期五"; break;
                case "Saturday": week = "星期六"; break;
                case "Sunday": week = "星期日"; break;
                default: week = ""; break;
            }

            return week;
        }


        /// <summary>
        /// 返回标准日期格式string(yyyy-MM-dd)
        /// </summary>
        public static string GetDate()
        {
            return DateTime.Now.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 返回标准时间格式string
        /// </summary>
        public static string GetTime()
        {
            return DateTime.Now.ToString("HH:mm:ss");
        }

        /// <summary>
        /// 返回标准时间格式string
        /// </summary>
        public static string GetDateTime()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 根据星期字符串获取DayOfWeek
        /// </summary>
        /// <param name="str">星期字符串</param>
        /// <returns>DayOfWeek</returns>
        public static DayOfWeek? GetDayOfWeek(string str)
        {
            DayOfWeek dow;
            switch (str)
            {
                case "Monday":
                    {
                        dow = DayOfWeek.Monday;
                    }
                    break;
                case "Tuesday":
                    {
                        dow = DayOfWeek.Tuesday;
                    }
                    break;
                case "Wednesday":
                    {
                        dow = DayOfWeek.Wednesday;
                    }
                    break;
                case "Thursday":
                    {
                        dow = DayOfWeek.Thursday;
                    }
                    break;
                case "Friday":
                    {
                        dow = DayOfWeek.Friday;
                    }
                    break;
                case "Saturday":
                    {
                        dow = DayOfWeek.Saturday;
                    }
                    break;
                case "Sunday":
                    {
                        dow = DayOfWeek.Sunday;
                    }
                    break;
                default:
                    {
                        return null;
                    }
                    break;
            }
            return dow;
        }

        /// <summary>
        /// 获取本周中的任意一天 
        /// </summary>
        /// <param name="dow">获取哪一天</param>
        /// <param name="dt">当前日期</param>
        /// <returns>本周的第几天的日期</returns>
        public static DateTime GetAnyDayOfWeek(DayOfWeek dow, DateTime dt)
        {
            DateTime temp = GetFirstDayOfWeek(dt);

            switch (dow)
            {
                case DayOfWeek.Monday:
                    {
                    }
                    break;
                case DayOfWeek.Tuesday:
                    {
                        temp = temp.AddDays(1);
                    }
                    break;
                case DayOfWeek.Wednesday:
                    {
                        temp = temp.AddDays(2);
                    }
                    break;
                case DayOfWeek.Thursday:
                    {
                        temp = temp.AddDays(3);
                    }
                    break;
                case DayOfWeek.Friday:
                    {
                        temp = temp.AddDays(4);
                    }
                    break;
                case DayOfWeek.Saturday:
                    {
                        temp = temp.AddDays(5);
                    }
                    break;
                case DayOfWeek.Sunday:
                    {
                        temp = temp.AddDays(6);
                    }
                    break;
            }

            return temp;
        }
    }

知识共享许可协议
《C# 时间操作类》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     148.35ms