【问题标题】:Getting first and last day of the current month获取当月的第一天和最后一天
【发布时间】:2014-03-22 07:30:47
【问题描述】:

我这里有 2 个日期选择器,用于 开始日期结束日期

如何获取当月的第一天和最后一天

rdpStartDate.SelectedDate = DateTime.Now;
rdpEndDate.SelectedDate = DateTime.Now;

【问题讨论】:

    标签: c# date


    【解决方案1】:
    DateTime now = DateTime.Now;
    var startDate = new DateTime(now.Year, now.Month, 1);
    var endDate = startDate.AddMonths(1).AddDays(-1);
    

    【讨论】:

    • 如果你想得到当月的最后时间,那么使用 AddTicks(-1) 而不是 AddDays(-1)
    • @InquisitorJax 在这种情况下,如果可能的话,我宁愿在你的逻辑中使用<> 而不是<=>=。例如,如果您的程序认为截止是<= 2019-10-31T23:59:59.9999999,但数据库服务器或远程服务器将截止解释为<= 2019-10-31T23:59:59.999,您可能会遇到其他系统的精确错误。如果您改为指定< 2019-11-01T00:00:00.0000000,则不会出现此问题。
    【解决方案2】:
    var now = DateTime.Now;
    var first = new DateTime(now.Year, now.Month, 1);
    var last = first.AddMonths(1).AddDays(-1);
    

    你也可以使用DateTime.DaysInMonth方法:

    var last = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month));
    

    【讨论】:

    • 领先7秒!嘘。 =)
    【解决方案3】:
    var myDate = DateTime.Now;
    var startOfMonth = new DateTime(myDate.Year, myDate.Month, 1);
    var endOfMonth = startOfMonth.AddMonths(1).AddDays(-1);
    

    这应该可以满足您的需求。

    【讨论】:

      【解决方案4】:

      试试这个已经用c#构建的代码

      int lastDay = DateTime.DaysInMonth (2014, 2);
      

      第一天总是1。

      祝你好运!

      【讨论】:

        【解决方案5】:

        另一种方法是使用DateTime.DaysInMonth 来获取@Jade 建议的当月的天数

        由于我们知道每月的第一天总是1,我们可以将其用作第一天的默认值,当前月份和年份为current.year,current.Month,1

        var now = DateTime.Now; // get the current DateTime 
        
        //Get the number of days in the current month
        int daysInMonth = DateTime.DaysInMonth (now.Year, now.Month); 
            
        //First day of the month is always 1
        var firstDay = new DateTime(now.Year,now.Month,1); 
            
        //Last day will be similar to the number of days calculated above
        var lastDay = new DateTime(now.Year,now.Month,daysInMonth);
        
        //So
        rdpStartDate.SelectedDate = firstDay;
        rdpEndDate.SelectedDate = lastDay; 
        

        【讨论】:

          【解决方案6】:
          string firstdayofyear = new DateTime(DateTime.Now.Year, 1, 1).ToString("MM-dd-yyyy");
          string lastdayofyear = new DateTime(DateTime.Now.Year, 12, 31).ToString("MM-dd-yyyy");
          string firstdayofmonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("MM-dd-yyyy");
          string lastdayofmonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1).ToString("MM-dd-yyyy");
          

          【讨论】:

          • 请在您的答案中添加一些解释性文字。为什么是字符串?这个答案如何改进已经发布的许多答案?
          • 这不会尝试以任何不同的方式回答问题,而是使用与上面回答相同的逻辑广告添加了不必要的 firstdayofyearlastdayofyear这里甚至没有被问到。
          猜你喜欢
          • 2017-01-09
          • 1970-01-01
          • 2016-02-09
          • 2019-03-04
          • 1970-01-01
          • 2016-08-18
          • 1970-01-01
          • 2015-04-03
          • 2013-11-25
          相关资源
          最近更新 更多