locked
Get First Date Of Last 4th Months RRS feed

  • Question

  • User-807418713 posted

    Hello

    Consider Today is 12-Jul-2019

    I want to show in textbox1 01-Apr-2019 i mean last 4 month first date as it goes so on.. 

    Thank You

    Friday, July 12, 2019 5:01 AM

Answers

  • User288213138 posted

    Hi Gopi.MCA,

    According to your description, this demo is for your reference.

    Get year and month based on today's date and set date to 1, Then subtract the number of months according to the requirement.

    The code:

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    var today = DateTime.Today;
                var month = new DateTime(today.Year, today.Month, 1);
                var first = month.AddMonths(-3);
                TextBox1.Text = first.ToString(("dd-MMM-yyyy"));
    

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 12, 2019 7:54 AM

All replies

  • User632428103 posted

    Hello,

    try something like this :

       DateTime GetFirstDateOfMonth(int month)
            {
                var date = DateTime.Now;
                var firstDayOfMonth = new DateTime(date.Year, date.Month, 1);
                return firstDayOfMonth.AddMonths(month);
            }

    Friday, July 12, 2019 7:35 AM
  • User288213138 posted

    Hi Gopi.MCA,

    According to your description, this demo is for your reference.

    Get year and month based on today's date and set date to 1, Then subtract the number of months according to the requirement.

    The code:

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    var today = DateTime.Today;
                var month = new DateTime(today.Year, today.Month, 1);
                var first = month.AddMonths(-3);
                TextBox1.Text = first.ToString(("dd-MMM-yyyy"));
    

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 12, 2019 7:54 AM