I have a bit of javascript that gets different date ranges and formats them to return yyyymmdd. I get the final result I need, but something about having 2 different variables bugs me and makes me think I am not doing this the best way. I am wondering if there is a way to pass the new date and the additional removal of - all in one line.
my function is:
function toJSONLocalMonth (firstDay, lastDay) {//set default date range to from beggining of this month
var local = new Date(firstDay);
local.setMinutes(firstDay.getMinutes() - firstDay.getTimezoneOffset());
return local.toJSON().slice(0, 10);
var local = new Date(lastDay);
local.setMinutes(lastDay.getMinutes() - lastDay.getTimezoneOffset());
return local.toJSON().slice(0, 10);
}
And whenever I need the result I do this:)example of today and yesterday)
var dateToday = new Date();
dateTodayFormat = toJSONLocalMonth(dateToday).replace(/-/g, "");//format date yyyymmdd
dateYesterday = dateToday.setDate(dateToday.getDate() - 1);
dateYesterdayFormat = toJSONLocalMonth(dateToday).replace(/-/g, "");
If there a better way to get this result, or at the very least combine the dateYesterday
and dateYesterdayFormat
to a single line to get yyymmdd.
(I need to keep the - in the function result, so I can't filter it there.)
Thanks!