日期格式转换--年 月 周数 日

/// <summary>
        /// 年 月 周数 日 字符串数组
        /// </summary>
        /// <param name="dt">需要转换的日期</param>
        /// <returns></returns>
        public string[] getYearMonthWeekDayOfDate(DateTime dt)
        {
            string[] YearMonthWeekDay = new string[4];
            YearMonthWeekDay[0] = dt.Year.ToString();
            YearMonthWeekDay[1] = dt.Month.ToString();
            YearMonthWeekDay[2] = "W" + getWeekNumberOfMonth(dt).ToString();
            YearMonthWeekDay[3] = dt.Day.ToString();
            return YearMonthWeekDay;
        }
        /// <summary>
        /// 获取日期的周数
        /// </summary>
        /// <param name="dt">需要转换的日期</param>
        /// <returns></returns>
        public int getWeekNumberOfMonth(DateTime dt)
        {
            DateTime monthStartDate = DateTime.Parse(dt.Year.ToString() + "-" + dt.Month.ToString() + "-" + "01");
            DateTime monthEndDate = monthStartDate.AddMonths(1);
            int difference = getWeekDifference(monthStartDate.DayOfWeek.ToString(), "Sunday");
            if (dt <= monthStartDate.AddDays(difference) && dt <= monthEndDate)
            {
                return 1;
            }
            else if (dt < monthStartDate.AddDays(difference + 7 + 1) && dt < monthEndDate)
            {
                return 2;
            }
            else if (dt < monthStartDate.AddDays(difference + 7 * 2 + 1) && dt < monthEndDate)
            {
                return 3;
            }
            else if (dt < monthStartDate.AddDays(difference + 7 * 3 + 1) && dt < monthEndDate)
            {
                return 4;
            }
            else if (dt < monthStartDate.AddDays(difference + 7 * 4 + 1) && dt < monthEndDate)
            {
                return 5;
            }
            else
            {
                return 6;
            }
        }
        /// <summary>
        /// 结果小于-7或大于7,传入参数错误
        /// </summary>
        /// <param name="start">起始DayOfWeek</param>
        /// <param name="end">结束DayOfWeek</param>
        /// <returns></returns>
        public int getWeekDifference(string start, string end)
        {
            int difference = getWeek(end) - getWeek(start);
            return difference;
        }
        /// <summary>
        /// DayOfWeek转week
        /// </summary>
        /// <param name="strWeek">DayOfWeek</param>
        /// <returns></returns>
        public week getWeek(string strWeek)
        {
            switch (strWeek)
            {
                case "Sunday":
                    return week.Sunday;
                case "Monday":
                    return week.Monday;
                case "Tuesday":
                    return week.Tuesday;
                case "Wednesday":
                    return week.Wednesday;
                case "Thursday":
                    return week.Thursday;
                case "Friday":
                    return week.Friday;
                case "Saturday":
                    return week.Saturday;
                default:
                    return week.Wrong;
            }
        }
        public enum week
        {
            /// <summary>
            /// 星期天
            /// </summary>
            Sunday = 7,
            /// <summary>
            /// 星期一
            /// </summary>
            Monday = 1,

            /// <summary>
            /// 星期二
            /// </summary>
            Tuesday = 2,

            /// <summary>
            /// 星期三
            /// </summary>
            Wednesday = 3,

            /// <summary>
            /// 星期四
            /// </summary>
            Thursday = 4,

            /// <summary>
            /// 星期五
            /// </summary>
            Friday = 5,

            /// <summary>
            /// 星期六
            /// </summary>
            Saturday = 6,
            /// <summary>
            ///
            /// </summary>
            Wrong = 0,
        }

转载于:https://www.cnblogs.com/chaodongwang/archive/2009/10/28/1591264.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值