Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out the previous year date from current date in javascript

Tags:

javascript

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
like image 469
user1519718 Avatar asked Jul 25 '12 12:07

user1519718


People also ask

How do I get this year in JavaScript?

To get the current year, use the getFullYear() JavaScript method. JavaScript date getFullYear() method returns the year of the specified date according to local time. The value returned by getFullYear() is an absolute number.

Does new date () Return current date?

Use new Date() to generate a new Date object containing the current date and time. This will give you today's date in the format of mm/dd/yyyy.

How do you find the year from the moment?

Calling format('YYYY') will invoke moment's string formatting functions, which will parse the format string supplied, and build a new string containing the appropriate data. Since you only are passing YYYY , then the result will be a string containing the year. If all you need is the year, then use the year() function.


2 Answers

You need to use getFullYear()

and then generate new Date

var d = new Date(2012, 7, 25); d.setFullYear(d.getFullYear() - 1); 
like image 74
Dor Cohen Avatar answered Oct 02 '22 09:10

Dor Cohen


Try this

var d = new Date(); var pastYear = d.getFullYear() - 1; d.setFullYear(pastYear); console.log(d); 
like image 21
Sharmila Avatar answered Oct 02 '22 10:10

Sharmila