asp.net关于时间方法,当前月有多少天、求某年有多少周、求当前日期是一年的中第几周

 

/// 当前月有多少天
        /// </summary>
        /// <param name="y"></param>
        /// <param name="m"></param>
        /// <returns></returns>
        public static int HowMonthDay(int y, int m)
         {
            int mnext;
            int ynext;
            if (m < 12)
             {
                 mnext = m + 1;
                 ynext = y;
             }
            else
             {
                 mnext = 1;
                 ynext = y + 1;
             }
             DateTime dt1 = System.Convert.ToDateTime(y + "-" + m + "-1");
             DateTime dt2 = System.Convert.ToDateTime(ynext + "-" + mnext + "-1");
             TimeSpan diff = dt2 - dt1;
            return diff.Days;
         }

        /**/
        /// <summary>
        /// 得到一年中的某周的起始日和截止日
        /// 年 nYear
        /// 周数 nNumWeek
        /// 周始 out dtWeekStart
        /// 周终 out dtWeekeEnd
        /// </summary>
        /// <param name="nYear"></param>
        /// <param name="nNumWeek"></param>
        /// <param name="dtWeekStart"></param>
        /// <param name="dtWeekeEnd"></param>
        public static void GetWeek(int nYear, int nNumWeek, out    DateTime dtWeekStart, out    DateTime dtWeekeEnd)
         {
             DateTime dt = new DateTime(nYear, 1, 1);
             dt = dt + new TimeSpan((nNumWeek - 1) * 7, 0, 0, 0);
             dtWeekStart = dt.AddDays(-(int)dt.DayOfWeek + (int)DayOfWeek.Monday);
             dtWeekeEnd = dt.AddDays((int)DayOfWeek.Saturday - (int)dt.DayOfWeek + 1);
         }

        /**/
        /// <summary>
        /// 求某年有多少周
        /// 返回 int
        /// </summary>
        /// <param name="strYear"></param>
        /// <returns>int</returns>
        public static int GetYearWeekCount(int strYear)
         {
             System.DateTime fDt = DateTime.Parse(strYear.ToString() + "-01-01");
            int k = Convert.ToInt32(fDt.DayOfWeek);//得到该年的第一天是周几
            if (k == 1)
             {
                int countDay = fDt.AddYears(1).AddDays(-1).DayOfYear;
                int countWeek = countDay / 7 + 1;
                return countWeek;

             }
            else
             {
                int countDay = fDt.AddYears(1).AddDays(-1).DayOfYear;
                int countWeek = countDay / 7 + 2;
                return countWeek;
             }

         }

        /**/
        /// <summary>
        /// 求当前日期是一年的中第几周
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static int WeekOfYear(DateTime curDay)
         {
            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;
             }
         }

        /// <summary>
        /// 返回相差的秒数
        /// </summary>
        /// <param name="Time"></param>
        /// <param name="Sec"></param>
        /// <returns></returns>
        public static int StrDateDiffSeconds(string Time, int Sec)
         {
             TimeSpan ts = DateTime.Now - DateTime.Parse(Time).AddSeconds(Sec);
            if (ts.TotalSeconds > int.MaxValue)
             {
                return int.MaxValue;
             }
            else if (ts.TotalSeconds < int.MinValue)
             {
                return int.MinValue;
             }
            return (int)ts.TotalSeconds;
         }
        /// <summary>
        /// 返回相差的分钟数
        /// </summary>
        /// <param name="time"></param>
        /// <param name="minutes"></param>
        /// <returns></returns>
        public static int StrDateDiffMinutes(string time, int minutes)
         {
            if (time == "" || time == null)
                return 1;
             TimeSpan ts = DateTime.Now - DateTime.Parse(time).AddMinutes(minutes);
            if (ts.TotalMinutes > int.MaxValue)
             {
                return int.MaxValue;
             }
            else if (ts.TotalMinutes < int.MinValue)
             {
                return int.MinValue;
             }
            return (int)ts.TotalMinutes;
         }
        /// <summary>
        /// 返回相差的小时数
        /// </summary>
        /// <param name="time"></param>
        /// <param name="hours"></param>
        /// <returns></returns>
        public static int StrDateDiffHours(string time, int hours)
         {
            if (time == "" || time == null)
                return 1;
             TimeSpan ts = DateTime.Now - DateTime.Parse(time).AddHours(hours);
            if (ts.TotalHours > int.MaxValue)
             {
                return int.MaxValue;
             }
            else if (ts.TotalHours < int.MinValue)
             {
                return int.MinValue;
             }
            return (int)ts.TotalHours;
         }

//一年中有多少周

int year = DateTime.Now.Year;

//The first day of this year
DateTime firstDate = new DateTime(year, 1, 1);
//The last day of this year
DateTime lastDate = new DateTime(year, 12, 31);

//The week of the firstDate
int firstWeek = Convert.ToInt32(firstDate.DayOfWeek);
//The week of the lastDate
int lastWeek = Convert.ToInt32(lastDate.DayOfWeek);

//Output the result
Console.WriteLine(firstWeek);
Console.WriteLine(lastWeek);

//Calculate days of the firstWeek and the lastWeek
int extraDays = 7 - firstWeek + lastWeek;
int mainDays = 365 - extraDays;

//If leap year,mainDays + 1
int judge = year % 4;
if (judge == 0) { mainDays++; }

int mainWeeks = mainDays / 7;
int weeks = 2 + mainWeeks;

Console.WriteLine(weeks.ToString());

//Pause the program
Console.Read();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

感谢一路走过的人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值