欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#后台的一些常用的公用方法--时间计算篇

程序员文章站 2024-03-17 15:51:52
...

1.获取当前周

//获取当前日期是本年度的第几周
int thisWeek = WeekOfYear(DateTime.Now.AddDays(1).ToString());

//获得当前周
        public static int WeekOfYear(string date)
        {
            DateTime curDay = Convert.ToDateTime(date);
            int firstdayofweek = Convert.ToInt32(Convert.ToDateTime(curDay.Year.ToString() + "- " + "1-1 ").DayOfWeek);
            int days = curDay.DayOfYear;
            int daysOutOneWeek = days - (7 - firstdayofweek);
            if (daysOutOneWeek <= 0)
            {
                return 1;
            }
            else
            {
                int weeks = daysOutOneWeek / 7;
                if (daysOutOneWeek % 7 != 0)
                    weeks++;
                return weeks + 1;
            }
        }

2.获取指定周数的第一天的日期和最后一天的日期(默认周日为一周的第一天)

//指定周的第一天和最后一天的日期
GetDaysOfWeeks(int.Parse(thisYear), thisWeek, out DateTime firstDay, out DateTime lastDay);

/// <summary>
        /// 获取指定周数的开始日期和结束日期,开始日期为周日
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="index">周数</param>
        /// <param name="first">当此方法返回时,则包含参数 year 和 index 指定的周的开始日期的 System.DateTime 值;如果失败,则为 System.DateTime.MinValue。</param>
        /// <param name="last">当此方法返回时,则包含参数 year 和 index 指定的周的结束日期的 System.DateTime 值;如果失败,则为 System.DateTime.MinValue。</param>
        /// <returns></returns>
        public static bool GetDaysOfWeeks(int year, int index, out DateTime first, out DateTime last)
        {
            first = DateTime.MinValue;
            last = DateTime.MinValue;
            if (year < 1700 || year > 9999)
            {
                //"年份超限"
                return false;
            }
            if (index < 1 || index > 53)
            {
                //"周数错误"
                return false;
            }
            DateTime startDay = new DateTime(year, 1, 1);  //该年第一天
            DateTime endDay = new DateTime(year + 1, 1, 1).AddMilliseconds(-1);
            int dayOfWeek = 0;
            if (Convert.ToInt32(startDay.DayOfWeek.ToString("d")) > 0)
                dayOfWeek = Convert.ToInt32(startDay.DayOfWeek.ToString("d"));  //该年第一天为星期几
            if (dayOfWeek == 7) { dayOfWeek = 0; }
            if (index == 1)
            {
                first = startDay;
                if (dayOfWeek == 6)
                {
                    last = first;
                }
                else
                {
                    last = startDay.AddDays((6 - dayOfWeek));
                }
            }
            else
            {
                first = startDay.AddDays((7 - dayOfWeek) + (index - 2) * 7); //index周的起始日期
                last = first.AddDays(6);
                if (last > endDay)
                {
                    last = endDay;
                }
            }
            if (first > endDay)  //startDayOfWeeks不在该年范围内
            {
                //"输入周数大于本年最大周数";
                return false;
            }
            return true;
        }

3.获取指定年月的第一天和最后一天的日期

//指定年月的第一天最后一天
string firstDayOfThisMonth = GetCurMonthFirstDay(DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString()).ToString("yyyyMMdd");
                string lastDayOfThisMonth = GetCurMonthLastDay(DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString()).ToString("yyyyMMdd");

//获取指定年月的第一天
        public static DateTime GetCurMonthFirstDay(string year, string mon)
        {
            DateTime AssemblDate = Convert.ToDateTime(year + "-" + mon + "-" + "01");  // 组装当前指定月份
            return AssemblDate.AddDays(1 - AssemblDate.Day);  // 返回指定当前月份的第一天
        }


        //获取指定年月的最后一天
        public static DateTime GetCurMonthLastDay(string year, string mon)
        {
            DateTime AssemblDate = Convert.ToDateTime(year + "-" + mon + "-" + "01");  // 组装当前指定月份
            return AssemblDate.AddDays(1 - AssemblDate.Day).AddMonths(1).AddDays(-1);  // 返回指定当前月份的最后一天
        }

 

相关标签: c#