1

I am looking for help with a javascript code that will allow me the predict a shipping arrival date for my customers.

For example if the customer orders an item on 12/14/13 I want the script the tell them that the item will arrive on XX/XX/XXXX. I want the script to calculate 14 business days from the purchase date.

Stephanie
  • 81
  • 1
  • 4
  • 11
  • What javascript code did you tried yet ? – cubitouch Jan 05 '14 at 15:44
  • I haven't tried anything yet. Not quite sure where to start. I am using Shopify and need to place this code in an html email. – Stephanie Jan 05 '14 at 15:45
  • Do you need to take holidays into account? – Michael Robinson Jan 05 '14 at 15:45
  • Yes I need to account for holidays and weekends. I own an online store and the shipping is 14 business days. I want to calculate the date of arrival excluding holidays and weekends – Stephanie Jan 05 '14 at 15:47
  • Have a look at this tutorial http://www.w3schools.com/js/js_obj_date.asp, and come back with what you could get to update your question ? =) (you will have to get an exhaustive list of the 'holidays') – cubitouch Jan 05 '14 at 15:47
  • 2
    You're not going to be able to rely on JavaScript code running in an HTML email. [See this question.](http://stackoverflow.com/questions/1088016/html-email-with-javascript) And @cubitouch [please be careful recommending w3schools](http://w3fools.com). – Pointy Jan 05 '14 at 15:48
  • @Pointy I learnt a lot on this site... it's not perfect or fully updated, but this is something for beginning with javascript... anyway this won't run into an email so... But thanks for the advice, I'll keep that in mind. – cubitouch Jan 05 '14 at 15:52
  • Are there any other alternatives for getting a date prediction like this in an email? – Stephanie Jan 05 '14 at 16:07
  • Would it be possible to create a php page on my site that a customer could input their order date and be returned a delivery date? – Stephanie Jan 05 '14 at 16:09
  • "I am looking for help" is not a terse question about programming. – Lightness Races in Orbit Jan 05 '14 at 16:50

1 Answers1

1

I have created a JSFiddle for this, here the link http://jsfiddle.net/8mad9/

If you want to just add 14 days (considering weekends), that's pretty straightforward.

var today = new Date();
var days = 14;
var deliveryDate = new Date(today.getTime() + (days * 24 * 60 * 60 * 1000));
console.log(deliveryDate);

if today is "Sun Jan 05 2014 16:45:44", will print out "Sun Jan 19 2014 16:46:56".

If we don't want to consider weekends we need to calculate add the weekend days.
For example, today is the 5th of Jan 2014, Sunday.

console.log(new Date().getDay());
--> 0

Sunday is 0, Monday is 1.... and Saturday is 6

Using this, we can use a loop to calculate the 14 business days in this way
var today = new Date(); //5th jan 2014
var business_days = 14;

var deliveryDate = today; //will be incremented by the for loop
var total_days = business_days; //will be used by the for loop
for(var days=1; days <= total_days; days++) {
   deliveryDate = new Date(today.getTime() + (days *24*60*60*1000));
   if(deliveryDate.getDay() == 0 || deliveryDate.getDay() == 6) {
     //it's a weekend day so we increase the total_days of 1
     total_days++
   }
} 
console.log(today);
console.log(deliveryDate);


--> Sun Jan 05 2014 17:13:39 GMT+0100 (CET)
--> Thu Jan 23 2014 17:13:39 GMT+0100 (CET)
Alvise Susmel
  • 569
  • 4
  • 9