I will get the date from the response of a service in the format YYYY-MM-DD .After extracting the date i need to subtract 28days from the extracted date.How to do it via javascript? The code which i wrote is below 1st scenario:- var ss="" var date="12/22/2017" var now=new Date(date); ss=now.setDate(now.getDate()-30) Output=1511308800000 2nd scenario:- if i put the code as var date="12/22/2017" var now=new Date(date); now.setDate(now.getDate()-30) output=Wed Nov 22 2017 00:00:00 GMT-0000 (UTC) in the 1st scenario,the output which i am getting is this because of i ma setting that to a variable? how should i assign in to a different variable? is there a better way to do this? Any help on this?
Solved! Go to Solution.
The JavaScript Date type has a getDate() method, which returns an integer number, between 1 and 31, representing the day of the month for the given date according to local time.
If you want a date from 28 days prior, you would do this:
var dateString = "12/22/2017"; var date1 = new Date(dateString); var daysPrior = 28; date1.setDate(date1.getDate() - daysPrior)); print (date1.toISOString());
Subtracting days may give you a "negative date", but the setDate() function allows for that and will adjust the month accordingly.
This is independent of Apigee Edge or Rhino. This works in any JavaScript engine.
The JavaScript Date type has a getDate() method, which returns an integer number, between 1 and 31, representing the day of the month for the given date according to local time.
If you want a date from 28 days prior, you would do this:
var dateString = "12/22/2017"; var date1 = new Date(dateString); var daysPrior = 28; date1.setDate(date1.getDate() - daysPrior)); print (date1.toISOString());
Subtracting days may give you a "negative date", but the setDate() function allows for that and will adjust the month accordingly.
This is independent of Apigee Edge or Rhino. This works in any JavaScript engine.
This works great.
If you make a little change, you'll get an even broader solution
var daysToMove = -28; date1.setDate(date1.getDate() + daysToMove);
Reference to Date.prototype.setDate() if in doubt
https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate
User | Count |
---|---|
2 | |
1 | |
1 | |
1 | |
1 |