Zum Hauptinhalt springen

getCalcDateTime

This function converts a date and time into a numeric value representing the number of seconds, minutes, or hours since the Unix epoch (January 1, 1970). Unlike getCalcDate, this function preserves the time component, making it suitable for precise time calculations.

Syntax

app.getCalcDateTime(dateTimeParam, accuracy)

Parameters

ParameterTypeDescription
dateTimeParamDate, String, or ObjectThe date and time to convert. Can be a JavaScript Date object, an ISO datetime string, or an object that can be converted to a date
accuracyString(Optional) The time unit for the result: "seconds" (default), "minutes", or "hours"

Return Value

Returns a Number representing the time units since the Unix epoch, in the specified accuracy.

Examples

Example 1: Calculate time difference in seconds:

// Calculate seconds between two timestamps
const startTime = app.getFieldValue("processStart");
const endTime = app.getFieldValue("processEnd");

const startValue = app.getCalcDateTime(startTime);
const endValue = app.getCalcDateTime(endTime);

const secondsDifference = endValue - startValue;
app.setFieldValue("processDuration", secondsDifference);

Example 2: Get current time in minutes for time tracking:

// Record start time in minutes for tracking
const startTimeMinutes = app.getCalcDateTime(new Date(), "minutes");
app.setFieldValue("taskStartTime", startTimeMinutes);

// Later calculate elapsed minutes
const currentTimeMinutes = app.getCalcDateTime(new Date(), "minutes");
const elapsedMinutes = currentTimeMinutes - app.getFieldValue("taskStartTime");

Example 3: Calculate hours between timestamps:

// Calculate working hours between clock-in and clock-out
const clockIn = app.getFieldValue("clockInTime");
const clockOut = app.getFieldValue("clockOutTime");

const hoursWorked = app.getCalcDateTime(clockOut, "hours") -
app.getCalcDateTime(clockIn, "hours");

app.setFieldValue("hoursWorked", hoursWorked.toFixed(2));

Example 4: Calculate if a timestamp is within working hours:

// Check if current time is within working hours (9:00 - 17:00)
const now = new Date();
const currentHours = now.getHours();
const currentMinutes = now.getMinutes();

// Calculate minutes since start of day
const minutesSinceMidnight = currentHours * 60 + currentMinutes;

// Define working hours in minutes since midnight
const workStart = 9 * 60; // 9:00 AM
const workEnd = 17 * 60; // 5:00 PM

// Check if current time is within working hours
const isWorkingHours = minutesSinceMidnight >= workStart &&
minutesSinceMidnight <= workEnd;

app.setFieldValue("isWorkingHours", isWorkingHours);
  • getCalcDate() - For date-only calculations (ignores time component)
  • getDate() - For formatting a date object as a string
  • getToday() - For getting today's date as a string

Notes

  • By default, the function returns the time in seconds since epoch
  • With "minutes" accuracy, seconds are truncated (set to 0)
  • With "hours" accuracy, both minutes and seconds are truncated (set to 0)
  • The resulting value can be used directly in arithmetic operations (addition, subtraction, comparison)
  • For storing timestamps that will be used in calculations, use this function rather than formatted date strings
  • When working with different time zones, be aware that the calculation uses the local time zone of the client