Can I Get Some Feedback? (Moment.js in particular)
Jack Harner 🚀
Posted on May 10, 2019
I'm setting up this thing for work where we would display on our website if a customers order is going to be shipped out today or on the next available business day. Our cut off is 12 Noon Mountain Time, and we only ship out M-F.
Here's the relevant code:
var now = moment().tz("America/Denver");
var availableDays = [1, 2, 3, 4, 5];
var todaysCutoff = now.clone().hour(12);
var expectedProcessing = "";
// If today is after 12 Noon, Expected Processing goes to tomorrow.
if (now > todaysCutoff) {
expectedProcessing = now.clone().add(1, 'day');
} else {
expectedProcessing = now.clone();
};
// If Expected Processing is not during the week, set to Monday of next week
if (!availableDays.includes(expectedProcessing.day())) {
expectedProcessing = expectedProcessing.add(1, 'week').day(1);
};
$("#processing").html("Expected Ship Date: " +
expectedProcessing.format("MMMM Do YYYY hh:mm")
);
Basically, if it's after Noon, set the Ship Date to the next day. If the Ship Date is a weekend, push it out to the next Monday.
As far as I can tell it works, was just curious about the feedback from someone more knowledgable than me on Moment.js/moment-timezone.
Thanks!
💖 💪 🙅 🚩
Jack Harner 🚀
Posted on May 10, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.