• asp.net 获取当月的第一天和最后一天示例


    ASP.NET(C#)返回每个月的第一天和最后一天要怎么来做呢,我们下文整理了不少例子,然后根据例子做了一个智能判断每个月有多少天,无需你计算,本函数将替你完成: 希望对各位有帮助。
     

    最简单获取当月第一天和最后一天 代码如下

     代码如下 复制代码

    DateTime now = DateTime.Now;
    DateTime dt1 = new DateTime(now.Year, now.Month, 1);

    DateTime dt2 = d1.AddMonths(1).AddDays(-1);

    dt1是本月的第一天,dt2本月的最后一天,
    最后一天的算法是:得到本月的第一天然后增加一月,再减去一天.

    例子

     代码如下 复制代码

    DateTime dt = DateTime.Now; 
    //本月第一天时间 
    DateTime dt_First = dt.AddDays(-(dt.Day) + 1); 
    Label1.Text = dt_First.ToString("yyyy-MM-dd"); 
    //将本月月数+1 
    DateTime dt2 = dt.AddMonths(1); 
    //本月最后一天时间 
    DateTime dt_Last = dt2.AddDays(-(dt.Day)); 
    Label2.Text = dt_Last.ToString("yyyy-MM-dd"); 
    int weeknow = Convert.ToInt32(System.DateTime.Now.DayOfWeek); 
    int daydiff = (-1) * weeknow + 1; 
    int dayadd = 7 - weeknow;

    上面我们都需要手来写,下面代码可以实现智能判断了

    例子,智能判断每个月有多少天,无需你计算,本函数将替你完成:

     代码如下 复制代码
    using System;
    namespace DotNet.Utilities
    {
        public class DateFormat
        {
            //返回每月的第一天和最后一天
            public static void ReturnDateFormat(int month, out string firstDay, out string lastDay)
            {
                int year = DateTime.Now.Year + month / 12;
                if (month != 12)
                {
                    month = month % 12;
                }
                switch (month)
                {
                    case 1:
                        firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                        lastDay = DateTime.Now.ToString(year + "-0" + month + "-31");
                        break;
                    case 2:
                        firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                        if (DateTime.IsLeapYear(DateTime.Now.Year))
                            lastDay = DateTime.Now.ToString(year + "-0" + month + "-29");
                        else
                            lastDay = DateTime.Now.ToString(year + "-0" + month + "-28");
                        break;
                    case 3:
                        firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                        lastDay = DateTime.Now.ToString("yyyy-0" + month + "-31");
                        break;
                    case 4:
                        firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                        lastDay = DateTime.Now.ToString(year + "-0" + month + "-30");
                        break;
                    case 5:
                        firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                        lastDay = DateTime.Now.ToString(year + "-0" + month + "-31");
                        break;
                    case 6:
                        firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                        lastDay = DateTime.Now.ToString(year + "-0" + month + "-30");
                        break;
                    case 7:
                        firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                        lastDay = DateTime.Now.ToString(year + "-0" + month + "-31");
                        break;
                    case 8:
                        firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                        lastDay = DateTime.Now.ToString(year + "-0" + month + "-31");
                        break;
                    case 9:
                        firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
                        lastDay = DateTime.Now.ToString(year + "-0" + month + "-30");
                        break;
                    case 10:
                        firstDay = DateTime.Now.ToString(year + "-" + month + "-01");
                        lastDay = DateTime.Now.ToString(year + "-" + month + "-31");
                        break;
                    case 11:
                        firstDay = DateTime.Now.ToString(year + "-" + month + "-01");
                        lastDay = DateTime.Now.ToString(year + "-" + month + "-30");
                        break;
                    default:
                        firstDay = DateTime.Now.ToString(year + "-" + month + "-01");
                        lastDay = DateTime.Now.ToString(year + "-" + month + "-31");
                        break;
                }
            }
        }
    }
  • 相关阅读:
    docker安装mtproto及报错解决方案
    Centos7下创建和管理用户
    GitHub项目绑定自己的域名
    navicate远程连接mysql8.0失败
    Java反射
    Spring AOP
    Spring注解
    学习进度笔记20
    学习进度笔记19
    学习进度笔记18
  • 原文地址:https://www.cnblogs.com/CandiceW/p/4290483.html
Copyright © 2020-2023  润新知