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 8993479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:12:12+00:00 2026-06-15T23:12:12+00:00

Possible Duplicate: Subtract days from a date in javascript I have got a JavaScript

  • 0

Possible Duplicate:
Subtract days from a date in javascript

I have got a JavaScript that basically returns a date that is 2 days ago. It is as follows:

var x;
var m_names = new Array("January", "February", "March", 
    "April", "May", "June", "July", "August", "September", 
    "October", "November", "December");

var d = new Date();
var twoDaysAgo = d.getDate()-2;  //change day here
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var x = twoDaysAgo + "-" + m_names[curr_month] + "-" + curr_year;

document.write(x);

Assuming today is 12-December-2012, the above will return the date 10-December-2012. I don’t think this will work dynamically as we move forward into a new month, OR, change the day from -2 to -15. It will work only from the 3rd of the month.

How can I modify this so when it is 12-December-2012 today and I want it to return me the date 15 days ago it should be 27-November-2012… and not -3-December-2012?

Any help appreciated. Thanks!
I’m a Javascript newbie.

  • 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-06-15T23:12:12+00:00Added an answer on June 15, 2026 at 11:12 pm

    If you have a date object, you can set it to two days previous by subtracting two from the date:

    var d = new Date();
    d.setDate(d.getDate() - 2);
    console.log(d.toString());
    
    // First of month
    var c = new Date(2017,1,1); // 1 Feb -> 30 Jan
    c.setDate(c.getDate() - 2);
    console.log(c.toString());
    
    // First of year
    var b = new Date(2018,0,1); // 1 Jan -> 30 Dec
    b.setDate(b.getDate() - 2);
    console.log(b.toString());
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I subtract a day from a python date? I have
Possible Duplicate: How to subtract X days from a date using Java calendar? This
Possible Duplicate: Finding date by subtracting X number of days from a particular date
Possible Duplicate: Anyone know a simple way using java calendar to subtract X days
Possible Duplicate: Remove duplicates from array Could you please help me out. I have
Possible Duplicate: Anyone know a simple way using java calendar to subtract X days
Possible Duplicate: c#: whats the easiest way to subtract time? Suppose I have this
Possible Duplicate: Subtract from an integer using jquery I want to subtract 50 from
Possible Duplicate: Why does ReSharper want to use 'var' for everything? I have ReSharper
Possible Duplicate: c#: whats the easiest way to subtract time? I have a table

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.