Gets the start and end dates for "last month" date-range, relative to a specified date. - CSharp System

CSharp examples for System:DateTime Month

Description

Gets the start and end dates for "last month" date-range, relative to a specified date.

Demo Code

// Copyright (c) .NET Foundation. All rights reserved.
using System.Windows.Forms;
using OpenLiveWriter.Interop.Windows;
using System.Runtime.InteropServices;
using System.Globalization;
using System.Diagnostics;
using System;/*from  w  ww  .  j  a va  2s.  co  m*/

public class Main{
        /// <summary>
        /// Gets the start and end dates for "last month" date-range, relative to a specified date.
        /// </summary>
        /// <param name="dateTime">The DateTime that the calculation is relative to.</param>
        /// <param name="start">Start date.</param>
        /// <param name="end">End date.</param>
        public static void GetLastMonthDateRange(DateTime dateTime, out DateTime start, out DateTime end)
        {
            dateTime = dateTime.Date;
            end = dateTime.AddDays(-dateTime.Day);
            start = end.AddDays(-(end.Day - 1));
        }
}

Related Tutorials