Skip to content

Instantly share code, notes, and snippets.

@devinbethel
Created August 25, 2021 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinbethel/52e1b5b35500f0762fdf3bf48e237759 to your computer and use it in GitHub Desktop.
Save devinbethel/52e1b5b35500f0762fdf3bf48e237759 to your computer and use it in GitHub Desktop.
// A loose, sloppy way to estimate
// dateObject.getEstimatedArrivalDate(integer shippingTransitDays)
/** Devin's JS for Estimating Arrival Times - don't touch! 1/21/21 **/
// Configure Shipping Cutoff Times (in user's local time?)
configShippingCutOffHour = 17;
configTransitDaysFreeShipping = 3;
configTransitDaysFastShipping = 1;
// Appends ordinal suffix to integer d, ie 3 => "3rd"
nth = function(d) {
if (d > 3 && d < 21) return 'th';
switch (d % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
/** Devin's JS for Estimating Arrival Times - don't touch! 1/21/21 **/
// Returns pretty date (ie "Friday, Jan 12")
Date.prototype.getArrivalDateString = function(){
return this.toLocaleString('en-us', {weekday:'short'}) + ", " + this.toLocaleString('en-us', {month:'short'}) + " " + this.getDate() + nth(this.getDate());
}
Date.prototype.getShippingCutOffHour = function(){ return this.shippingCutOffHour || 12; }
Date.prototype.setShippingCutOffHour = function(h){ this.shippingCutOffHour = h; }
Date.prototype.isBeforeShippingCutOff = function(){
return this.getShippingCutOffHour() > this.getHours();
}
// Used for Urgency
// returns {"hours": 12, "minutes": 13}
Date.prototype.getTimeUntilShippingCutOff = function(){
if(!this.timeUntilShippingCutOff){
var hShippingCutOffHour = this.getShippingCutOffHour();
hShippingCutOffHour -= this.getMinutes() == 0 ? 0 : 1;
var h = hShippingCutOffHour < this.getHours() ? 24 + hShippingCutOffHour - this.getHours() : hShippingCutOffHour - this.getHours();
var m = this.getMinutes() == 0 ? 59 : 59 - this.getMinutes();
this.timeUntilShippingCutOff = {hours: h, minutes: m};
}
return this.timeUntilShippingCutOff;
}
// Input: Integer, number of days carrier provides for SLA (ie "priority 2-day" => 2)
// Returns new Date object that can be manipulated to estimate arrival timinig info
Date.prototype.getEstimatedArrivalDate = function( shippingTransitDays ){
var d = shippingTransitDays;
var arrivalDate = new Date( this ); // copy date.
// Calc days until the order leaves the FC
var days_until_transit_start = 0;
if(arrivalDate.getDay()>0 && arrivalDate.getDay()<4){
days_until_transit_start = arrivalDate.isBeforeShippingCutOff() ? 0 : 1;
} else if (arrivalDate.getDay() == 5 && arrivalDate.isBeforeShippingCutOff()){
days_until_transit_start = 0;
} else if (arrivalDate.getDay() == 5) {
days_until_transit_start = 2;
} else if (arrivalDate.getDay() == 6) {
days_until_transit_start = 1;
} else if (arrivalDate.getDay() == 0) {
days_until_transit_start = 0;
}
arrivalDate.setDate(arrivalDate.getDate() + days_until_transit_start + shippingTransitDays );
// Ensure that arrival date is not on a Sunday; change to Monday
if(arrivalDate.getDay() == 0)
arrivalDate.setDate(arrivalDate.getDate() + 1);
return arrivalDate;
}
// dummy var, could be a form field input
//OrderDate = new Date();
//var deliveryDate = OrderDate.getEstimatedArrivalDate(3);
// expected output 2020-02-25
//console.log("Order date: %s, Delivery date: %s",OrderDate,deliveryDate );
var updateEstimatedArrivalTimes = function(){
var today = new Date();
var freeShipping = today.getEstimatedArrivalDate(configTransitDaysFreeShipping);
var fastShipping = today.getEstimatedArrivalDate(configTransitDaysFastShipping);
// update text for free shipping HTML elements
for(var i = 0, r = document.getElementsByClassName('arrival-date-free-shipping'); !!r && i < r.length; i++)
r[i].innerText = freeShipping.getArrivalDateString();
// update text for fast shipping HTML elements
for(var i = 0, fr = document.getElementsByClassName('arrival-date-fast-shipping'); !!fr && i < fr.length; i++)
fr[i].innerText = fastShipping.getArrivalDateString();
// update text for cut off time HTML elements
var dbg;
for(var i = 0, fr = document.getElementsByClassName('arrival-date-cutoff-time-remaining'); !!fr && i < fr.length; i++){
var remaining = fastShipping.getTimeUntilShippingCutOff();
fr[i].getElementsByClassName('hours')[0].innerHTML = remaining.hours;
fr[i].getElementsByClassName('minutes')[0].innerHTML = remaining.minutes;
}
}
updateEstimatedArrivalTimes();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment