Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6756639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:32:56+00:00 2026-05-26T13:32:56+00:00

I Am trying to form a date which is 3 months before the current

  • 0

I Am trying to form a date which is 3 months before the current date. I get the current month by the below code

var currentDate = new Date();
var currentMonth = currentDate.getMonth()+1;

Can you guys provide me the logic to calculate and form a date (an object of the Date data type) considering that when the month is January (1), 3 months before date would be OCtober (10)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:32:56+00:00Added an answer on May 26, 2026 at 1:32 pm
    var d = new Date();
    d.setMonth(d.getMonth() - 3);
    

    This works for January. Run this snippet:

    var d = new Date("January 14, 2012");
    console.log(d.toLocaleDateString());
    d.setMonth(d.getMonth() - 3);
    console.log(d.toLocaleDateString());

    There are some caveats…

    A month is a curious thing. How do you define 1 month? 30 days? Most people will say that one month ago means the same day of the month on the previous month citation needed. But more than half the time, that is 31 days ago, not 30. And if today is the 31st of the month (and it isn’t August or Decemeber), that day of the month doesn’t exist in the previous month.

    Interestingly, Google agrees with JavaScript if you ask it what day is one month before another day:

    Google search result for 'one month before March 31st' shows 'March 3rd'

    It also says that one month is 30.4167 days long:
    Google search result for 'one month in days' shows '30.4167'

    So, is one month before March 31st the same day as one month before March 28th, 3 days earlier? This all depends on what you mean by “one month before”. Go have a conversation with your product owner.

    If you want to do like momentjs does, and correct these last day of the month errors by moving to the last day of the month, you can do something like this:

    const d = new Date("March 31, 2019");
    console.log(d.toLocaleDateString());
    const month = d.getMonth();
    d.setMonth(d.getMonth() - 1);
    while (d.getMonth() === month) {
        d.setDate(d.getDate() - 1);
    }
    console.log(d.toLocaleDateString());

    If your requirements are more complicated than that, use some math and write some code. You are a developer! You don’t have to install a library! You don’t have to copy and paste from stackoverflow! You can develop the code yourself to do precisely what you need!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write code for registration form which have the following fields.
I'm trying to insert the current date from a php form into a database.
Im trying to get a form working in Internet Explorer. I see that when
Im trying to submit a specific form programatically, but I allways get the initial
im trying to create a semi-transparent form which is displayed in a panel. i
I'm new to Django, trying to process some forms. I have this form for
i am trying to retrieve date from Mysql db to my html form in
I am trying to create a form which loads upon a user clicking a
I am trying to open a form having a DateTextField which must be populated
I am trying to get machine-parsable bus schedule data by submitting a form with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.