Table of Contents

Date and time one line snippets

When working with time, BusinessMate knows 4 different types of time; 

– Date without hours

– Datetime, including date and hours

– Time, only with hours

– Duration

Finding time sped, 

time passed

 

Defining deadline, 

Filtering before or after 

it all require caclulating with time.

If you can’t find the solution you need here – ask us at qa@businessmate.io

 

Working with minutes and seconds

now() | datePlus({minutes: 45})

 

addSeconds(date, amount)

dateFns.addSeconds(new Date(),10)

addMinutes(date, amount)

 

interval/duration

 

https://date-fns.org/v2.23.0/docs/

Working with hours

addHours(date, amount)

subHours(date, amount)

differenceInHours(dateLeft, dateRight)

Working with days

differenceInDays(dateLeft, dateRight)

subDays(date, amount)

Weeks

addWeeks(date, amount)

subWeeks(date, amount)

dateFns.addDays(new Date(),10)

Working with month, quarters, years

‘2021-10-01’ | datePlus({years:1})

addMonths(date, amount)

addQuarters(date, amount)

addYears(date, amount)

now() | datePlus({months: 3})

 

				
					const dateFns = require('date-fns');

// now + 10 sec
dateFns.addSeconds(new Date(),10)

// now - 10 sec
dateFns.subSeconds(new Date(),10)

// now + 10 min
dateFns.addMinutes(new Date(),10)

// now - 10 min
dateFns.subMinutes(new Date(),10)

				
			
				
					const dateFns = require('date-fns');

// now + 10 hours
dateFns.addHours(new Date(),10)

// now - 10 hours
dateFns.subHours(new Date(),10)

// differnce between to date time
// result is shown in 
dateFns.differenceInHours(dateLeft,dateRight)



				
			
				
					// today + 10 days
dateFns.addDays(new Date(),10)

// today - 10 days
dateFns.subDays(new Date(),10)

// today + 14 days
dateFns.addweeks(new Date(),2)

// today - 14 days
dateFns.subweeks(new Date(),2)


				
			
				
					const dateFns = require('date-fns');

// today + 2 months
dateFns.addMonths(new Date(),2)

// today + 3 months
dateFns.addQuarters(new Date(),1)

// today + 2 years
dateFns.addYears(new Date(),2)

				
			

Special cases

set a deadline from a workflow, here it is now + 12 days

 

Validate time remaining to deadline

 

Measure the duration from now to deadline, initially in milisecornds, then converted to hours

 

Sending a mail from a workflow, the platform is working in UTC (London time). It the workflow is to send a mail with a date og a point in time, you need to adjust for that, it can be done like this sample.

 

present a stored time (UTC)  in your timezone .

 

 

 

 

 

 

 

 

				
					const dateFns = require('date-fns');

//  set deadline to now plus 12 addDays

currentRecord.f_deadline =
dateFns.addDays(new Date(),12);


// how long time to deadline
var now= new Date().toISOString()

//z is duration in miliseconds
variables.duration = ( Date.parse( currentRecord.f_deadline ) – Date.parse( now))

// duration is diff in hours
variables.z = variables.z/1000/60/60;

// Present the date in your timezone
var localdate = new Date(currentRecord.f_dato)=setHours(dat.getHours() + 2);
      localdate.setHours(localdate.getHours() + 2);
      
      

				
			

Working with timezones

All dates and times are stored as datetime in UTC, the Z timezone.

Therefor when you store a Date, being in central europe, it will store one hour before midnight of the date you store.

If you are in daylight saving normally 1 hour earlier than the timezone, then it will still store the equvelant point in time for UTC, for CET that is 2 hours earlier.

store the 4 November 2022, is 2022-11-04 23:00:00:000z in the database

All workflows are running in the same timezone (UTC).

If you need a point in time or a date in specific timezone, then you can use the following methods to convert it.

First you should find the timezone you would like to work with, you can find it here

https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

You should use the TZ database name as reference for the timezone you will work with.

 

 

 

 

				
					var intervalToDuration = require('date-fns/intervalToDuration')

//Duration as JSON object
variables.t  =   intervalToDuration({
  start: new Date(),
  end: new Date(currentRecord.f_deadline)
})

//{"days":22,"hours":8,"years":51,"months":9,"minutes":30,"seconds":3}

// available as array
variables.t["days"];

variables.t["days"] + variables.t["hours"] +variables.t["minutes"]+ variables.t["seconds"]


				
			

Find the 3th last working day of the month

This script find the n’th last working day if you provide a day (currentRecord.f_Date) in the month and the numbers of workingdays till the end of the month (currentRecord.f_lastdate)

 

 

 

 

				
					const lastDayOfMonth = require('date-fns/lastDayOfMonth')
const dateFns = require('date-fns');

var indate = currentRecord.f_date  // the inserted date
var lastday = currentRecord.f_lastdate // the n'th last working day of the month
var dayinweek =dateFns.getDay(new Date(lastDayOfMonth(new Date(indate))));
var nthdate // the date of the n'th last working day of the month 

if(dayinweek <= lastday ){
    nthdate=  dateFns.subDays( lastDayOfMonth(new Date(indate)),lastday+2-1);
}else{
    nthdate= dateFns.subDays(lastDayOfMonth(new Date(indate)),lastday-1);
}

currentRecord.f_deadline = nthdate