码迷,mamicode.com
首页 > 其他好文 > 详细

根据当期时间得到本周第一天和最后一天

时间:2015-10-22 10:44:24      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

/// <summary>
        /// 根据指定接收本周第一天和最后一天
        /// </summary>
        /// <param name="day">当前日期</param>
        /// <param name="sDate">本周第一天</param>
        /// <param name="eDate">本周最后一天</param>
        public static void GetWeekStarAndEnd(DateTime day, out DateTime sDate, out DateTime eDate)
        {
        
            sDate = DateTime.MinValue;
            eDate = DateTime.MinValue;
            switch (day.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    sDate = Convert.ToDateTime(day.ToString("yyyy-MM-dd"));
                    eDate = new DateTime(sDate.Year, sDate.AddDays(6).Month, sDate.AddDays(6).Day, 23, 59, 59);
                    break;
                case DayOfWeek.Tuesday:
                    sDate = Convert.ToDateTime(day.AddDays(-1).ToString("yyyy-MM-dd"));
                    eDate = new DateTime(sDate.Year, sDate.AddDays(6).Month, sDate.AddDays(6).Day, 23, 59, 59);
                    break;
                case DayOfWeek.Wednesday:
                    sDate = Convert.ToDateTime(day.AddDays(-2).ToString("yyyy-MM-dd"));
                    eDate = new DateTime(sDate.Year, sDate.AddDays(6).Month, sDate.AddDays(6).Day, 23, 59, 59);
                    break;
                case DayOfWeek.Thursday:
                    sDate = Convert.ToDateTime(day.AddDays(-3).ToString("yyyy-MM-dd"));
                    eDate = new DateTime(sDate.Year, sDate.AddDays(6).Month, sDate.AddDays(6).Day, 23, 59, 59);
                    break;
                case DayOfWeek.Friday:
                    sDate = Convert.ToDateTime(day.AddDays(-4).ToString("yyyy-MM-dd"));
                    eDate = new DateTime(sDate.Year, sDate.AddDays(6).Month, sDate.AddDays(6).Day, 23, 59, 59);
                    break;
                case DayOfWeek.Saturday:
                    sDate = Convert.ToDateTime(day.AddDays(-5).ToString("yyyy-MM-dd"));
                    eDate = new DateTime(sDate.Year, sDate.AddDays(6).Month, sDate.AddDays(6).Day, 23, 59, 59);
                    break;
                case DayOfWeek.Sunday:
                    sDate = Convert.ToDateTime(day.AddDays(-6).ToString("yyyy-MM-dd"));
                    eDate = new DateTime(sDate.Year, sDate.AddDays(6).Month, sDate.AddDays(6).Day, 23, 59, 59);
                    break;
            }
        }

        /// <summary>
        /// 根据指定时间计算旬起始日期
        /// </summary>
        /// <param name="day">当前日期</param>
        /// <param name="sDate">旬开始日期</param>
        /// <param name="eDate">旬结束日期</param>
        /// <returns>返回旬(上旬,中旬,下旬)</returns>
        public static string Xun(DateTime day, out DateTime sDate, out DateTime eDate)
        {
            string ss = "";
            sDate = DateTime.MinValue;
            eDate = DateTime.MinValue;

            if (day.Day > 0 && day.Day < 11)
            {
                sDate = new DateTime(day.Year, day.Month, 1);
                eDate = new DateTime(day.Year, day.Month, 10, 23, 59, 59);
                ss = "上旬";
            }

            if (day.Day > 10 && day.Day < 21)
            {
                sDate = new DateTime(day.Year, day.Month, 11);
                eDate = new DateTime(day.Year, day.Month, 20, 23, 59, 59);
                ss = "中旬";
            }

            if (day.Day > 20 && day.Day <= 31)
            {
                sDate = new DateTime(day.Year, day.Month, 21);
                eDate = new DateTime(day.Year, day.Month, Convert.ToDateTime(day.AddMonths(1).ToString("yyyy-MM-01")).AddDays(-1).Day, 23, 59, 59);

                ss = "下旬";
            }

            return ss;
        }

        /// <summary>
        /// 计算季度
        /// </summary>
        /// <param name="day">当前日期</param>
        /// <param name="sDate">季度开始日期</param>
        /// <param name="eDate">季度结束日期</param>
        /// <returns>季度日期</returns>
        public static string JiDu(DateTime day, out DateTime sDate, out DateTime eDate)
        {
            string ss = "";

            sDate = DateTime.MinValue;
            eDate = DateTime.MinValue;

            if (day.Month > 0 && day.Month < 4)
            {
                sDate = new DateTime(day.Year, 1, 1);
                ss = "第一季度";
            }

            if (day.Month > 3 && day.Month < 7)
            {
                sDate = new DateTime(day.Year, 4, 1);
                ss = "第二季度";
            }

            if (day.Month > 7 && day.Month < 10)
            {
                sDate = new DateTime(day.Year, 7, 1);
                ss = "第三季度";
            }

            if (day.Month > 10 && day.Month <= 12)
            {
                sDate = new DateTime(day.Year, 10, 1);
                ss = "第四季度";
            }

            eDate = Convert.ToDateTime(sDate.AddMonths(3).ToString("yyyy-MM-01")).AddDays(-1);

            return ss;
        }

根据当期时间得到本周第一天和最后一天

标签:

原文地址:http://www.cnblogs.com/dachuang/p/4899932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!