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;
            }
        }
    }
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。