Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subtract 1 week from current date - javascript

Tags:

javascript

This is how I am getting current date, dd-MMM-yyyy format. How do I subtract 1 week.

    var m_names = new Array("JAN", "FEB", "MAR",
            "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");

    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();
    var current = curr_date + "-" + m_names[curr_month] + "-" + curr_year;
like image 389
Atish Avatar asked Jun 10 '26 07:06

Atish


2 Answers

Just add d.setDate(d.getDate() - 7); after your var d = new Date();

 var m_names = new Array("JAN", "FEB", "MAR",
     "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");
 var d = new Date();
 d.setDate(d.getDate() - 7);
 var curr_date = d.getDate();
 var curr_month = d.getMonth();
 var curr_year = d.getFullYear();
 var current = curr_date + "-" + m_names[curr_month] + "-" + curr_year;

jsFiddle example

like image 200
j08691 Avatar answered Jun 12 '26 21:06

j08691


You may try like this:-

var d= new Date();
d.setDate(d.getDate() - 7);

Using Date.js you can do like this:

Date.parse("t - 7 d").toString("MM-dd-yyyy");     
Date.today().addDays(-7).toString("MM-dd-yyyy");  
Date.today().addWeeks(-1).toString("MM-dd-yyyy");
like image 20
Rahul Tripathi Avatar answered Jun 12 '26 21:06

Rahul Tripathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!