Get the previous month's first and last day dates in c#

后端 未结 10 1452
眼角桃花
眼角桃花 2020-12-02 06:28

I can\'t think of an easy one or two liner that would get the previous months first day and last day.

I am LINQ-ifying a survey web app, and they squeezed a new requ

相关标签:
10条回答
  • 2020-12-02 06:49

    This is a take on Mike W's answer:

    internal static DateTime GetPreviousMonth(bool returnLastDayOfMonth)
    {
        DateTime firstDayOfThisMonth = DateTime.Today.AddDays( - ( DateTime.Today.Day - 1 ) );
        DateTime lastDayOfLastMonth = firstDayOfThisMonth.AddDays (-1);
        if (returnLastDayOfMonth) return lastDayOfLastMonth;
        return firstDayOfThisMonth.AddMonths(-1);
    }
    

    You can call it like so:

    dateTimePickerFrom.Value = GetPreviousMonth(false);
    dateTimePickerTo.Value = GetPreviousMonth(true);
    
    0 讨论(0)
  • 2020-12-02 06:50
    DateTime now = DateTime.Now;
    int prevMonth = now.AddMonths(-1).Month;
    int year = now.AddMonths(-1).Year;
    int daysInPrevMonth = DateTime.DaysInMonth(year, prevMonth);
    DateTime firstDayPrevMonth = new DateTime(year, prevMonth, 1);
    DateTime lastDayPrevMonth = new DateTime(year, prevMonth, daysInPrevMonth);
    Console.WriteLine("{0} {1}", firstDayPrevMonth.ToShortDateString(),
      lastDayPrevMonth.ToShortDateString());
    
    0 讨论(0)
  • 2020-12-02 06:57
    DateTime LastMonthLastDate = DateTime.Today.AddDays(0 - DateTime.Today.Day);
    DateTime LastMonthFirstDate = LastMonthLastDate.AddDays(1 - LastMonthLastDate.Day);
    
    0 讨论(0)
  • 2020-12-02 06:59

    The way I've done this in the past is first get the first day of this month

    dFirstDayOfThisMonth = DateTime.Today.AddDays( - ( DateTime.Today.Day - 1 ) );
    

    Then subtract a day to get end of last month

    dLastDayOfLastMonth = dFirstDayOfThisMonth.AddDays (-1);
    

    Then subtract a month to get first day of previous month

    dFirstDayOfLastMonth = dFirstDayOfThisMonth.AddMonths(-1);
    
    0 讨论(0)
  • 2020-12-02 07:00

    An approach using extension methods:

    class Program
    {
        static void Main(string[] args)
        {
            DateTime t = DateTime.Now;
    
            DateTime p = t.PreviousMonthFirstDay();
            Console.WriteLine( p.ToShortDateString() );
    
            p = t.PreviousMonthLastDay();
            Console.WriteLine( p.ToShortDateString() );
    
    
            Console.ReadKey();
        }
    }
    
    
    public static class Helpers
    {
        public static DateTime PreviousMonthFirstDay( this DateTime currentDate )
        {
            DateTime d = currentDate.PreviousMonthLastDay();
    
            return new DateTime( d.Year, d.Month, 1 );
        }
    
        public static DateTime PreviousMonthLastDay( this DateTime currentDate )
        {
            return new DateTime( currentDate.Year, currentDate.Month, 1 ).AddDays( -1 );
        }
    }
    

    See this link http://www.codeplex.com/fluentdatetime for some inspired DateTime extensions.

    0 讨论(0)
  • 2020-12-02 07:00

    The canonical use case in e-commerce is credit card expiration dates, MM/yy. Subtract one second instead of one day. Otherwise the card will appear expired for the entire last day of the expiration month.

    DateTime expiration = DateTime.Parse("07/2013");
    DateTime endOfTheMonthExpiration = new DateTime(
      expiration.Year, expiration.Month, 1).AddMonths(1).AddSeconds(-1);
    
    0 讨论(0)
提交回复
热议问题