day calculation in YT js workflow

Hi all, 

i have a js workflow. It calculates the days between now/today and field Due date. But it calculate based on seconds or more smaller unit. 

here is the script

exports.rule = entities.Issue.onSchedule({
 title: 'Upcoming Deadline: 30-Day Reminder for Operational Task', 
 search: '#ops-30-day-reminder-v2 has: {Due Date} Resolution: Open',
 cron: "0 58 10 ? * *", 
 guard: (ctx) => {
   const issue = ctx.issue;
   
   console.log(`Running guard for issue: ${issue.id}`);
   if (issue.fields.State !== entities.State.valueOf('Closed')) {  
     const dueDate = issue.fields.DueDate;
     const now = Date.now();
     
     console.log(`Due date: ${dueDate}, Now: ${now}`);
     const daysUntilDue = Math.ceil((dueDate - now) / (1000 * 60 * 60 * 24)); 
     console.log(`Days until due: ${daysUntilDue}`);
     
     return daysUntilDue === 30;
   }
   return false; 
 },
 action: (ctx) => {
   const issue = ctx.issue;
   const formattedDate = dateTime.format(issue.fields.DueDate, "MMM dd, yyyy");
   const taskDetails = `Task Name: ${issue.summary}<br>Due Date: ${formattedDate}<br>`;

here is the log of day calculation: i set up the due date is 25.12.2024, and today is 26.11.2024. It sopposed to be 29 days. 

workflow_user_2014333296 26 Nov 2024 10:58
Due date: 1735128000000, Now: 1732615080037

But the result is 30 days:

workflow_user_2014333296 26 Nov 2024 10:58
Days until due: 30

Is there a way to change it only calculate days but not deep into seconds?

Thx in advance!

0
1 comment

Hi! Math.ceil() rounds up to the nearest whole number (29.1 → 30). If you need to round down (29.1 → 29), you could use Math.floor() instead. You could also combine the two methods, depending on your requirements.

0

Please sign in to leave a comment.