# Javascript to calculate times

**URL:** <https://community.glideapps.com/t/javascript-to-calculate-times/68925>\
**Category:** Ask for Help\
**Created:** [December 21, 2023, 1:56pm UTC](https://community.glideapps.com/t/javascript-to-calculate-times/68925 "2023-12-21T13:56:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bman737](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@bman737](https://community.glideapps.com/u/bman737)\
**Post date:** [December 21, 2023, 1:56pm UTC](https://community.glideapps.com/t/javascript-to-calculate-times/68925/1 "2023-12-21T13:56:25Z")

</div>

I’m creating a javascript function for a project to calculate estimated delivery dates of orders based on # of pages, and whether it’s expedited or not. I also need the function to move any delivery dates that fall on Sunday to Monday at 10AM. I have the code but it’s not working and system doesn’t give me an error code. Does anyone have any suggestions?

In the code P1 is the date the order was created. I’ve tried the date in ISO format and now MMM dd, yyyy format but nothing. P2 is # of Pages and P3 is whether it’s expedited “Yes”, regular processing “No”, or 2 hour delivery.

CODE:  
function calculateDeliveryTime(p1, p2, p3) {

// Parse orderDate and numOfPages  
const [month, day, year] = p1.split(’ '); // Assuming input like “Dec 22 2023”  
const orderDate = new Date(`${month} ${day}, ${year}`);  
const numOfPages = Math.ceil(p2); // Rounding up decimal values

// Define fulfillment times in hours for different page ranges  
const fulfillmentTimes = {  
‘No’: { ‘1.0-3.0’: 24, ‘4.0-8.0’: 48, ‘9.0-15.0’: 72, ‘16.0-20.0’: 96 },  
‘Yes’: { ‘1.0-3.0’: 12, ‘4.0-8.0’: 24, ‘9.0-15.0’: 48, ‘16.0-20.0’: 72 },  
‘2 Hour Delivery (1-3 Pages Only)’: { ‘1.0-3.0’: 2 }  
};

// Function to get the fulfillment time for a given page range  
const getFulfillmentTime = () =\> {  
if (numOfPages \<= 3) return fulfillmentTimes[p3][‘1.0-3.0’];  
if (numOfPages \< 8) return fulfillmentTimes[p3][‘4.0-8.0’];  
if (numOfPages \<= 15) return fulfillmentTimes[p3][‘9.0-15.0’];  
if (numOfPages \<= 20) return fulfillmentTimes[p3][‘16.0-20.0’];  
if (numOfPages \<= 30) return fulfillmentTimes[p3][‘21.0-30.0’];  
return null;  
};

let fulfillmentTime = getFulfillmentTime();

if (fulfillmentTime === null) return ‘TBD’;

// Calculate delivery date  
let deliveryDate = new Date(orderDate);  
deliveryDate.setHours(deliveryDate.getHours() + fulfillmentTime);

// Adjust for Sundays  
if (deliveryDate.getDay() === 0) {  
deliveryDate.setDate(deliveryDate.getDate() + 1); // Move to Monday  
deliveryDate.setHours(10, 0, 0, 0); // Set time to 10:00 AM  
}

// Format the date in Month Day Year format  
const formattedDeliveryDate = deliveryDate.toLocaleDateString(‘en-US’, { month: ‘short’, day: ‘numeric’, year: ‘numeric’ });

return formattedDeliveryDate;  
}

---

<div class="post-metadata">

**Author:** ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)\
**Post date:** [December 21, 2023, 2:07pm UTC](https://community.glideapps.com/t/javascript-to-calculate-times/68925/2 "2023-12-21T14:07:34Z")

</div>

I don’t see anything that calls the function. It’s just sitting there with nothing to execute it. I would probably just take all of the code out of function if you don’t need it to be inside of one.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [December 21, 2023, 2:10pm UTC](https://community.glideapps.com/t/javascript-to-calculate-times/68925/3 "2023-12-21T14:10:13Z")

</div>

Somebody needs to tell ChatGPT how the Glide JavaScript column works 😂

---

<div class="post-metadata">

**Author:** ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)\
**Post date:** [December 21, 2023, 2:12pm UTC](https://community.glideapps.com/t/javascript-to-calculate-times/68925/4 "2023-12-21T14:12:41Z")

</div>

Agreed, but 90% of the code samples out there are shown with console.log so ChatGPT doesn’t know any better.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [December 21, 2023, 2:13pm UTC](https://community.glideapps.com/t/javascript-to-calculate-times/68925/5 "2023-12-21T14:13:51Z")

</div>

Yes, good point.  
hmm… perhaps a Community post is needed with tips for using ChatGPT generated code…

---

<div class="post-metadata">

**Author:** ![bman737](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@bman737](https://community.glideapps.com/u/bman737)\
**Post date:** [December 21, 2023, 2:14pm UTC](https://community.glideapps.com/t/javascript-to-calculate-times/68925/6 "2023-12-21T14:14:46Z")

</div>

yes, i’m not a coder. I learn with the resources that I have and the documentation for this platform is horrific to say the least 😕

---

<div class="post-metadata">

**Author:** ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)\
**Post date:** [December 21, 2023, 2:18pm UTC](https://community.glideapps.com/t/javascript-to-calculate-times/68925/7 "2023-12-21T14:18:10Z")

</div>

Hopefully you understood what I meant. Remove the first line with ‘function’ in it, and the last line that had the closing bracket for the function.

---

<div class="post-metadata">

**Author:** ![bman737](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@bman737](https://community.glideapps.com/u/bman737)\
**Post date:** [December 21, 2023, 2:20pm UTC](https://community.glideapps.com/t/javascript-to-calculate-times/68925/8 "2023-12-21T14:20:08Z")

</div>

I did, thank you!

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [December 21, 2023, 2:26pm UTC](https://community.glideapps.com/t/javascript-to-calculate-times/68925/9 "2023-12-21T14:26:47Z")

</div>

Not sure I’d agree with that. The [documentation](https://www.glideapps.com/docs) is far from perfect, for sure. But it’s still pretty good. And the [Glide University](https://learn.glideapps.com/dl/home) is a great learning resource 🤷‍♂️

That said, the JavaScript column isn’t even mentioned in the Glide Docs at all. Whilst teaching JavaScript is way outside the scope of the Glide Documentation, the docs should at least explain how it works and perhaps give a few usage examples. I have some small influence in this area, I’ll see what I can do.

---

<div class="post-metadata">

**Author:** ![bman737](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@bman737](https://community.glideapps.com/u/bman737)\
**Post date:** [December 21, 2023, 2:57pm UTC](https://community.glideapps.com/t/javascript-to-calculate-times/68925/10 "2023-12-21T14:57:39Z")

</div>

That’s what I meant. I always read extensively before posting and while this particular issue is obviously lack of basic coding knowledge on my part, if you put yourself in my shoes and type Java in Glide Docs, there is nothing there. The platform also doesn’t generate a error message. I usually rely on the forum posts (usually solved by you actually), but even then they are often obsolete.

A forum post with ChatGPT guidelines would be helpful considering most Glide users are beginners and ChatGPT is a great resource to get around the limitations of the platform.

---

<div class="post-metadata">

**Author:** ![nathanaelb](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/nathanaelb/32/43079_2.png) [@nathanaelb](https://community.glideapps.com/u/nathanaelb)\
**Post date:** [December 21, 2023, 3:26pm UTC](https://community.glideapps.com/t/javascript-to-calculate-times/68925/11 "2023-12-21T15:26:51Z")

</div>

> [@bman737](#):
>
> I usually rely on the forum posts (usually solved by you actually), but even then they are often obsolete.

If even the world can’t rely on Darren’s expertise, then we’re in trouble 😨
