zoukankan      html  css  js  c++  java
  • 【C#开发中常用方法】日期计算

    /// <summary>
    /// 获取指定日期,在为一年中为第几周
    /// </summary>
    /// <param name="dt">指定时间</param>
    /// <reutrn>返回第几周</reutrn>
    private static int GetWeekOfYear(DateTime dt)
    {
       GregorianCalendar gc = new GregorianCalendar();
       int weekOfYear = gc.GetWeekOfYear(dt,CalendarWeekRule.FirstDay, DayOfWeek.Monday);
       return weekOfYear;
    }

    /// <summary>
    /// 求某年有多少周
    /// 返回 int
    /// </summary>
    /// <param name="strYear"></param>
    /// <returns>int</returns>
    public static int GetYearWeekCount(int strYear)
    {
        string returnStr = "";
        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>
    /// 得到一年中的某周的起始日和截止日
    /// 年 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)
    {
        string returnStr = "";
        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;
        }
    }

  • 相关阅读:
    C++ primer plus读书笔记——第16章 string类和标准模板库
    C++ primer plus读书笔记——第15章 友元、异常和其他
    C++ primer plus读书笔记——第14章 C++中的代码重用
    C++ primer plus读书笔记——第13章 类继承
    C++ primer plus读书笔记——第12章 类和动态内存分配
    开发中常用的一些神器推荐
    收集常用的Linux常用命令
    【数据库】13种会导致索引失效语句写法
    Windows终端利器Cmder
    嵌入式操作系统的主要特点都有哪些
  • 原文地址:https://www.cnblogs.com/LiCoco/p/6183608.html
Copyright © 2011-2022 走看看