0

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!

Ionko Gueorguiev
  • 302
  • 4
  • 18

1 Answers1

1

Your question is unclear.

toJSONLocalMonth will only return one value, the one from the first return statement. The second is never reached.

The return value from dateToday.setDate(...) is a time value (a number), not a Date so you can't chain a string method to it. It modifies the date itself so dateYesterday is redundant, i.e.

dateYesterday = dateToday.setDate(dateToday.getDate() - 1);
dateYesterdayFormat = toJSONLocalMonth(dateToday).replace(/-/g, ""); 

can be:

dateToday.setDate(dateToday.getDate() - 1);
var dateYesterdayFormat = toJSONLocalMonth(dateToday).replace(/-/g, ""); 

The toJSONLocalMonth seems to be just getting a date string formatted as YYYYMMDD. I guess you're avoiding the built–in ISO methods because they use UTC/GMT and not the local time zone. The following function does that in a more obvious way:

/*  Return an ISO 8601 formatted date string
**  @param {Date} d - date to create string for
**  @returns {string} string formatted as ISO 8601 without timezone
*/
function toISOStringLocal(d) {
  function z(n){return (n<10?'0':'') + n}
  return d.getFullYear() + '-' + z(d.getMonth()+1) + '-' +
         z(d.getDate()) + 'T' + z(d.getHours()) + ':' +
         z(d.getMinutes()) + ':' + z(d.getSeconds())
          
}

console.log(toISOStringLocal(new Date));

You might also consider a small formatting library like fecha.js where you'd do:

var dateToday = new Date();
var dateTodayFormat = fecha.format(dateToday, 'YYYYMMDD')
dateToday.setDate(dateToday.getDate() - 1);
var dateYesterdayFormat = fecha.format(dateToday, 'YYYYMMDD');

The last two lines could become one using:

var dateYesterdayFormat = fecha.format(new Date(dateToday.setDate(dateToday.getDate() - 1)), 'YYYYMMDD');

but I wouldn't recommend that.

Also see: Where can I find documentation on formatting a date in JavaScript?

Community
  • 1
  • 1
RobG
  • 142,382
  • 31
  • 172
  • 209
  • Sorry if my question wasn't very clear, but you have helped me quite a bit in optimizing this function and understanding how it works instead of relying on the "magic" :) Thanks! – Ionko Gueorguiev Nov 22 '16 at 18:07