Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[JavaScript] Subtract month from date in format yyyyMMdd

//Input String yyyymmdd
var date_string = '20200101'

//Create date object
var oDate = new Date(date_string.substr(0, 4), date_string.substr(4, 2), date_string.substr(6, 2))

oDate.setMonth(oDate.getMonth() - 1)

var year = oDate.getFullYear().toString()
var month = oDate.getMonth().toString()
var day = oDate.getDay().toString()

//Add leading zero's
if (month.length === 1 ) {
	month = '0' + month
}
if (day.length === 1 ) {
	day = '0' + day
}

//Return string: 20191201
var sDate =  year + month + day

Leave a Reply

Your email address will not be published. Required fields are marked *