zoukankan      html  css  js  c++  java
  • C# 日期帮助类【原创】

          因项目需要简单的写了个日期帮助类,备忘一下!

    C#-CODE//============================================================
    // Producnt name:  BoBoARTS.CodeMad
    // Version:    1.0
    // Author:       董广祥
    // Auto generated at:  2009-8-7 13:36:20
    //============================================================
    using System;

    /// <summary>
    /// 日期帮助类
    /// </summary>
    public static class Utility
    {
        //当天
        public static string NowTime()
        {
            return DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
        }

        //当天日期减去相应天数
        public static string GetTimeByDayReduce(double day)
        {
            return DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.AddDays(day).Day.ToString();
        }

        //指定月份年份的第一天
        public static DateTime GetFirstDayOfMonth(int Year, int Month)
        {
            return Convert.ToDateTime(Year.ToString() + "-" + Month.ToString() + "-1");
        }

        //指定月份年份的最后一天
        public static DateTime GetLastDayOfMonth(int Year, int Month)
        {
            int Days = DateTime.DaysInMonth(Year, Month);
            return Convert.ToDateTime(Year.ToString() + "-" + Month.ToString() + "-" + Days.ToString());
        }

        //当前一周时间
        public static void ThisWeek(out string bDate, out string eDate)
        {
            DateTime firstDay = Convert.ToDateTime(NowTime());
            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();
        }

        //过去一周时间
        public static void BeforeWeek(out string bDate, out string eDate)
        {

            DateTime firstDay = Convert.ToDateTime(GetTimeByDayReduce(-ThisWeekLastDay() + 1));
            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();
        }

        //本周最后一天
        public static double ThisWeekLastDay()
        {
            DateTime firstDay = Convert.ToDateTime(NowTime());
            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; }
            return 7 - theday;
        }

        //本月第一天
        public static DateTime GetFirstDayOfMonthTime()
        {
            return GetFirstDayOfMonth(DateTime.Now.Year, DateTime.Now.Month);
        }

        //本月最后一天
        public static DateTime GetLastDayOfMonthTime()
        {
            return GetLastDayOfMonth(DateTime.Now.Year, DateTime.Now.Month);
        }

        //上月第一天
        public static DateTime GetFirstDayOfBeforeMonthTime()
        {
            return GetFirstDayOfMonth(DateTime.Now.Year, DateTime.Now.Month - 1);
        }

        //上月最后一天
        public static DateTime GetLastDayOfBeforeMonthTime()
        {
            return GetLastDayOfMonth(DateTime.Now.Year, DateTime.Now.Month - 1);
        }
        //根据下拉框状态获取相应的时间
        public static void GetTime(out string lastDateTime, out string firstDateTime, string state)
        {
            switch (state)
            {
                case "": firstDateTime = ""; lastDateTime = "";
                    break;
                case "today": firstDateTime = NowTime(); lastDateTime = "";
                    break;
                case "yesterday": firstDateTime = NowTime(); lastDateTime = GetTimeByDayReduce(-1);
                    break;
                case "last7days": firstDateTime = NowTime(); lastDateTime = GetTimeByDayReduce(-7);
                    break;
                case "thisweek": ThisWeek(out firstDateTime, out lastDateTime);
                    break;
                case "lastweek": BeforeWeek(out firstDateTime, out lastDateTime);
                    break;
                case "thismonth": firstDateTime = GetFirstDayOfMonthTime().ToString(); lastDateTime = GetLastDayOfMonthTime().ToString();
                    break;
                case "lastmonth": firstDateTime = GetFirstDayOfBeforeMonthTime().ToString(); lastDateTime = GetLastDayOfBeforeMonthTime().ToString();
                    break;
                default: firstDateTime = ""; lastDateTime = "";
                    break;
            }
        }
    }

  • 相关阅读:
    定位--position属性
    浮动
    超链接伪类
    对齐方式
    字体样式
    标准文档流
    CSS
    表单
    框架
    内部类
  • 原文地址:https://www.cnblogs.com/myssh/p/1557071.html
Copyright © 2011-2022 走看看