C#对时间的操作

    //获取当前星期的开始日期和结束日期
    private void TimeBE(out string bDate, out string eDate)
    {
        string firstDateText = NowTime();
        DateTime firstDay = Convert.ToDateTime(firstDateText);
        double theday;
        if (firstDay.DayOfWeek == DayOfWeek.Sunday) { theday = 7; }
        else if (firstDay.DayOfWeek == DayOfWeek.Monday) { theday = 1; }
        else if (firstDay.DayOfWeek == DayOfWeek.Tuesday) { theday = 2; }
        else if (firstDay.DayOfWeek == DayOfWeek.Wednesday) { theday = 3; }
        else if (firstDay.DayOfWeek == DayOfWeek.Thursday) { theday = 4; }
        else if (firstDay.DayOfWeek == DayOfWeek.Friday) { theday = 5; }
        else { theday = 6; }
        double bday = -theday;
        double eday = 7 - theday;
        bDate = firstDay.AddDays(bday).ToString();
        eDate = firstDay.AddDays(eday).ToString();
    }
    //获取当前月份的第一天
    private DateTime GetFirstDayOfMonthTime()
    {
        return GetFirstDayOfMonth(DateTime.Now.Year, DateTime.Now.Month);
    }
    //获取当前月份的最后一天
    private DateTime GetLastDayOfMonthTime()
    {
        return GetLastDayOfMonth(DateTime.Now.Year, DateTime.Now.Month);
    }
    //获取当前日期
    private string NowTime()
    {
        return DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
    }
    //根据年份月份获取月份的第一天
    private DateTime GetFirstDayOfMonth(int Year, int Month)
    {
        return Convert.ToDateTime(Year.ToString() + "-" + Month.ToString() + "-1");
    }
    //根据年份月份获取月份的最后一天
    private DateTime GetLastDayOfMonth(int Year, int Month)
    {
        int Days = DateTime.DaysInMonth(Year, Month);
        return Convert.ToDateTime(Year.ToString() + "-" + Month.ToString() + "-" + Days.ToString());
    }

你可能感兴趣的:(C#)