How to get a date three months ago in javascript in the format how YYYYDDMM is effective

I know a way to do in java:

Calendar c5 = Calendar.getInstance(); c5.add(Calendar.MONTH, -6); c5.getTime(); //It will give YYYYMMDD format three months ago. 

Is there any way to do this in javascript. I know that I can use Date d = new date (); analyze it and make a code to get the format. But now I do not want to understand and receive three months ago.

+6
source share
1 answer
 var dt = new Date('13 June 2013'); dt.setMonth(dt.getMonth()-1) 

Then you can use this code snippet this answer to convert it to YYYYMMDD

  Date.prototype.yyyymmdd = function() { var yyyy = this.getFullYear().toString(); var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based var dd = this.getDate().toString(); return yyyy + (mm[1]?mm:"0"+mm[0]) + (dd[1]?dd:"0"+dd[0]); // padding }; d = new Date(); d.yyyymmdd(); 

Something needs to be careful. If you are in March 31 and subtract a month, what happens? You can not get February 31! See this for more details.

+17
source

Source: https://habr.com/ru/post/918018/


All Articles