• 获取月份的周时间段


    包括C#方法和SQL方法。

    C#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("输入日期,并回车(0退出):");
                string d1 = Console.ReadLine();
                while (d1 != "0")
                {
                    DateTime dt = Convert.ToDateTime(d1);
                    List<MyDate> datelist = DateUtil.GetWeekOfMonth(dt);
                    Console.WriteLine(dt.Year+""+dt.Month+"月包含【"+datelist.Count+"】周:");
                    foreach (var d in datelist)
                    {
                        Console.WriteLine(d.firstDate + "	" + d.lastDate);
                    }
                    d1 = Console.ReadLine();
                }
            }
        }
    
        class DateUtil{
            #region 获取时间(周,月)
            public static DateTime GetWeekDateStart(DateTime? date)
            {
                DateTime currentdate = date.HasValue ? date.Value : DateTime.Now;
                //本周
                int weeknow = Convert.ToInt32(currentdate.DayOfWeek);
                //星期日 获取weeknow为0
                weeknow = weeknow == 0 ? 7 : weeknow;
                int daydiff = (-1) * weeknow + 1;
                //本周第一天
                return currentdate.AddDays(daydiff);
            }
            public static DateTime GetWeekDateEnd(DateTime? date)
            {
                DateTime currentdate = date.HasValue ? date.Value : DateTime.Now;
                //本周
                int weeknow = Convert.ToInt32(currentdate.DayOfWeek);
                //星期日 获取weeknow为0
                weeknow = weeknow == 0 ? 7 : weeknow;
                int dayadd = 7 - weeknow;
                //本周最后一天
                return currentdate.AddDays(dayadd);
            }
            public static DateTime GetMonthDateStart(DateTime? date)
            {
                DateTime currentdate = date.HasValue ? date.Value : DateTime.Now;
                //本月第一天时间 
                DateTime firstDate = currentdate.AddDays(-(currentdate.Day) + 1);
                return firstDate;
            }
            public static DateTime GetMonthDateEnd(DateTime? date)
            {
                DateTime currentdate = date.HasValue ? date.Value : DateTime.Now;
                DateTime dt = currentdate;
                //将本月月数+1 
                DateTime dt2 = dt.AddMonths(1);
                //本月最后一天时间 
                DateTime lastDate = dt2.AddDays(-(dt.Day));
                return lastDate;
            }
    
            /// <summary>
            /// 获取月份内的周时间段
            /// </summary>
            /// <param name="crdate">时间</param>
            /// <returns>当月周时间段</returns>
            public static List<MyDate> GetWeekOfMonth(DateTime? crdate)
            {
                List<MyDate> dateList = new List<MyDate>();
    
                DateTime currentdate = crdate.HasValue ? crdate.Value : DateTime.Now;
                DateTime firstDayofMonth = GetMonthDateStart(currentdate);
                DateTime lastDayofMonth = GetMonthDateEnd(currentdate);
    
                DateTime Week1firstday = GetWeekDateStart(firstDayofMonth);
                Week1firstday = Week1firstday < firstDayofMonth ? firstDayofMonth : Week1firstday;
                
                //第一周
                MyDate date1 = new MyDate();
                date1.firstDate = Week1firstday;
                date1.lastDate = GetWeekDateEnd(firstDayofMonth);
                dateList.Add(date1);
                //第二周
                MyDate date2 = new MyDate();
                DateTime Week2firstday = Week1firstday.AddDays(7);
                date2.firstDate = GetWeekDateStart(Week2firstday); ;
                date2.lastDate = GetWeekDateEnd(Week2firstday);
                dateList.Add(date2);
                //第三周
                MyDate date3 = new MyDate();
                DateTime Week3firstday = Week2firstday.AddDays(7);
                date3.firstDate = GetWeekDateStart(Week3firstday); ;
                date3.lastDate = GetWeekDateEnd(Week3firstday);
                dateList.Add(date3);
                //第四周
                MyDate date4 = new MyDate();
                DateTime Week4firstday = Week3firstday.AddDays(7);
                if (Week4firstday > lastDayofMonth)
                { Week4firstday = lastDayofMonth; }
                date4.firstDate = GetWeekDateStart(Week4firstday);
                date4.lastDate = GetWeekDateEnd(Week4firstday);
                if (date4.firstDate > lastDayofMonth)
                {
                    return dateList;
                }
                if (date4.lastDate >= lastDayofMonth)
                {
                    date4.lastDate = lastDayofMonth;
                    dateList.Add(date4);
                    return dateList;
                }
                dateList.Add(date4);
                //第五周
                MyDate date5 = new MyDate();
                DateTime Week5firstday = Week4firstday.AddDays(7);
                if (Week5firstday > lastDayofMonth)
                { Week5firstday = lastDayofMonth; }
                date5.firstDate = GetWeekDateStart(Week5firstday);
                date5.lastDate = GetWeekDateEnd(Week5firstday);
                if (date5.firstDate > lastDayofMonth)
                {
                    return dateList;
                }
                if (date5.lastDate >= lastDayofMonth)
                {
                    date5.lastDate = lastDayofMonth;
                    dateList.Add(date5);
                    return dateList;
                }
                dateList.Add(date5);
                //第六周
                MyDate date6 = new MyDate();
                DateTime Week6firstday = Week5firstday.AddDays(7);
                if (Week6firstday > lastDayofMonth)
                { Week6firstday = lastDayofMonth; }
                date6.firstDate = GetWeekDateStart(Week6firstday);
                date6.lastDate = GetWeekDateEnd(Week6firstday);
                if (date6.firstDate > lastDayofMonth)
                {
                    return dateList;
                }
                if (date6.lastDate >= lastDayofMonth)
                {
                    date6.lastDate = lastDayofMonth;
                    dateList.Add(date6);
                    return dateList;
                }
                dateList.Add(date6);
                return dateList;
            }
            #endregion
        }
    
        class MyDate
        {
            public DateTime firstDate { get; set; }
            public DateTime lastDate { get; set; }
        }
    }

    测试示例:

    SQL

    DECLARE @ym VARCHAR(7)
    SET @ym = '2013-12'
    SET datefirst 1
    SELECT  wk ,
            MIN(d) d1 ,
            MAX(d) d2
    FROM    ( SELECT    DATEPART(wk, d) wk ,
                        d
              FROM      ( SELECT    DATEADD(D, number, @ym + '-01') d
                          FROM      master..spt_values
                          WHERE     type = 'p'
                                    AND CONVERT(VARCHAR(7), DATEADD(D, number,
                                                                  @ym + '-01'), 120) = @ym
                        ) t
            ) t
    GROUP BY wk

    测试示例:

    另mark周几获取方法

    public string Week()
            {
                string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
                string week = weekdays[Convert.ToInt32(DateTime.Now.DayOfWeek)];
                return week;
            }
  • 相关阅读:
    外设驱动库开发笔记42:DAC8552 DAC驱动
    网络爬虫例子
    http 426 Upgrade Required
    springframework的Assert功能举例
    springframework的ReflectionUtils反射工具类功能举例
    Spring的RestTemplate功能举例
    cocos 3 如何发送全局自定义事件
    cocos 制作滚动按钮 聊天框的方式
    coocs 中的scrollView控件
    graalvm 编译原生java 解决反射的问题 maven配置
  • 原文地址:https://www.cnblogs.com/wxblog/p/3848063.html
Copyright © 2020-2023  润新知