44

I need to find out the previous year date from current date and then set as minDate in jQuery UIdatepicker in javascript

My date formaqt is dd-mm-yy

Ie

  • current date is 25-07-2012
  • I need to get 25-07-2011
1
  • 4
    And what if it is a leap year and you are on the leap day? Commented Jul 25, 2012 at 13:01

12 Answers 12

67

You need to use getFullYear()

and then generate new Date

var d = new Date(2012, 7, 25);
d.setFullYear(d.getFullYear() - 1);
Sign up to request clarification or add additional context in comments.

2 Comments

jan=0 so jul=6 for some absurd reason with js months
21

Try this

var d = new Date();
var pastYear = d.getFullYear() - 1;
d.setFullYear(pastYear);
console.log(d);

Comments

13

this solution will help.

new Date(new Date().setFullYear(new Date().getFullYear() - 1));

Comments

5

Use this :

 var date = new Date();
 var intYear = date.getFullYear() - 1;

Comments

3

Datepicker allows you to put a number as the minDate option, and it uses that as an offset from the current date. So you can write:

minDate: -365

to specify 1 year ago. This doesn't take leap years into account, though.

Comments

2

For strings:

curdate.substr(0, 6)+(curdate.substr(6)-1);

If you'd use a Date object, you could easily subtract a year with the set[Full]Year method.

Comments

1

you can define a new date or an existing date as d variable

var d = new Date();

then you can put date, month and year to the new date string using ${variableName}, Also you must add 1 to d.getMonth() and substract 1 from d.getFullYear()

var previousYearDate = `${d.getDate()}-${d.getMonth() + 1}-${d.getFullYear() - 1}`;

3 Comments

It's better to add some details around your answer.
Yeah... I will do it :)
ela machan this is nice
0
var today = new Date();
var curyear = today.getFullYear();
var curyearMonth = today.getMonth() + 1;
var curyearDay = today.getDate();
var lastYear = curyear - 1;
if ((curyearMonth == 2) && (curyearDay == 29)) {
    curyearDay = 28;
}

var lastYearDisplay = ("0000" + lastYear.toString()).slice(-4) + "-" + ("00" + curyearMonth.toString()).slice(-2) + "-" + ("00" + curyearDay.toString()).slice(-2);
alert("LastWeekDate : " + lastYearDisplay);

Comments

0

I found this very useful:

new Date(new Date().setFullYear(new Date().getFullYear() - 1)).toISOString().slice(0, 10);

Comments

-1

To avoid the Date object (if that is what OP wishes):

var currDate = '25-07-2012';
var dateParts = currDate.split('-');
dateParts[2] = parseInt(dateParts[2], 10) - 1;
alert(dateParts.join('-'));​

1 Comment

Will not properly work with the leap year and 29th of Feb
-1
var date = CALC.today(); //(function in my dev-enviro - gives today's date)
var year = formatDate(date, 'yyyy'); //year is 2016
year = parseInt(year);               //make sure '2016' becomes an integer
year = year -1;                      // var year is now: 2015.

//its been a while, but that's what I did from memory.

Comments

-1
  function getTodayDate() {
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth() + 1; //January is not 0!
        var yyyy = today.getFullYear();
        if (dd < 10) { dd = '0' + dd }
        if (mm < 10) { mm = '0' + mm }
        today = yyyy + '-' + mm + '-' + dd;
        return today;
    };
    function getYearAgo(){
    var lastYear = new Date();
    var dd = lastYear.getDate();
    var mm = lastYear.getMonth() + 1; //January is not 0!
    var yyyy = lastYear.getFullYear(getTodayDate) - 1;
    if (dd < 10) { dd = '0' + dd }
    if (mm < 10) { mm = '0' + mm }
    lastYear = yyyy + '-' + mm + '-' + dd;     
    return lastYear;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.