eLabSDK2.DateUtilities.Date

Hierarchy

  • default

    Date

Methods

formatDateTime

Static formatDateTime(date): string

Format a JavaScript Date object according to the current user's date and time preferences.

This converts a JavaScript Date object into a formatted string using the date format, time format, and timezone configured in the current user's preferences. The formatting automatically applies the user's locale settings. Use this to display dates and times consistently throughout the application in a way that matches user expectations.

Parameters

NameTypeDescription
dateJsDateA JavaScript Date object or date-like object to format.

Returns

string

A formatted datetime string according to the user's preferences (e.g., '11/27/2024 3:45 PM').

Example

// Format current date and time
const now = new Date();
const formatted = eLabSDK2.Date.formatDateTime(now);
console.log(formatted); // Output depends on user preferences, e.g., '27-11-2024 15:45'

Example

// Format a specific date
const specificDate = new Date('2024-11-27T15:30:00');
const formatted = eLabSDK2.Date.formatDateTime(specificDate);
document.getElementById('timestamp').textContent = formatted;

Example

// Display experiment timestamp
const experimentCreated = new Date(experiment.createdAt);
const displayTime = eLabSDK2.Date.formatDateTime(experimentCreated);
console.log(`Experiment created: ${displayTime}`);

getJsDateFromTimeString

Static getJsDateFromTimeString(dateTimeString): JsDate

Convert a date/time string to a JavaScript Date object using the user's date/time format preferences.

This parses a datetime string according to the current user's configured date and time format settings and timezone, returning a JavaScript Date object. Supports the special value '=NOW()' for current datetime. Use this when you receive datetime strings from user input or formatted displays and need to work with them as Date objects.

Parameters

NameTypeDescription
dateTimeStringstringA datetime string formatted according to the user's preferences, or '=NOW()' for current time.

Returns

JsDate

A JavaScript Date object representing the parsed datetime.

Example

// Parse user-formatted datetime string
const userInputString = '27-11-2024 15:30'; // Format depends on user preferences
const dateObject = eLabSDK2.Date.getJsDateFromTimeString(userInputString);
console.log(dateObject.toISOString());

Example

// Use current time
const now = eLabSDK2.Date.getJsDateFromTimeString('=NOW()');
console.log(`Current time: ${now}`);

Example

// Parse and manipulate date
const dateString = '15-03-2024 14:00';
const date = eLabSDK2.Date.getJsDateFromTimeString(dateString);
date.setHours(date.getHours() + 1); // Add one hour
console.log(`One hour later: ${date}`);

Example

// Convert form input to Date object
const input = document.getElementById('datetime-input').value;
const date = eLabSDK2.Date.getJsDateFromTimeString(input);
if (date < new Date()) {
  alert('Please select a future date');
}

userDateTimeToUTC

Static userDateTimeToUTC(dateTime, format, timezone): string

Convert a datetime from a user's timezone to UTC format.

This takes a datetime in the user's local timezone and converts it to UTC representation using the specified format. Supports the special value '=NOW()' for current datetime. Use this when submitting datetime data to the server, which expects UTC timestamps.

Parameters

NameTypeDescription
dateTimestringAn ISO datetime string (e.g., '1985-10-26T22:00:00Z') or the special value '=NOW()' for current time.
formatstringA moment.js compatible date format string. See docs.
timezonestringThe user's timezone according to the IANA timezone database (e.g., 'Europe/Amsterdam', 'America/New_York').

Returns

string

The datetime converted to UTC and formatted according to the specified format string.

Example

// Convert user datetime to UTC
const userDateTime = '2024-11-27T15:30:00';
const utcDateTime = eLabSDK2.Date.userDateTimeToUTC(
  userDateTime,
  'YYYY-MM-DD HH:mm:ss',
  'Europe/Amsterdam'
);
console.log(utcDateTime); // '2024-11-27 14:30:00' (UTC)

Example

// Use current time
const currentUTC = eLabSDK2.Date.userDateTimeToUTC(
  '=NOW()',
  'YYYY-MM-DD HH:mm:ss',
  'America/New_York'
);

userDateToServerDateTime

Static userDateToServerDateTime(jsDate): string

Convert a user's local datetime to the server's expected datetime format.

This takes a JavaScript Date object in the user's timezone and converts it to the datetime format expected by the server (typically UTC or server timezone). Accounts for timezone offsets to ensure accurate datetime submission. Use this when sending datetime data to the backend via API calls to ensure proper timezone handling.

Parameters

NameTypeDescription
jsDateJsDateA JavaScript Date object in the user's local timezone.

Returns

string

A formatted datetime string in the server's expected format.

Example

// Submit datetime to server
const userSelectedTime = new Date('2024-11-27T15:30:00');
const serverTime = eLabSDK2.Date.userDateToServerDateTime(userSelectedTime);
await api.createBooking({ startTime: serverTime });

Example

// Convert form input to server format
const dateInput = document.getElementById('date-picker').valueAsDate;
const serverDateTime = eLabSDK2.Date.userDateToServerDateTime(dateInput);
console.log(`Sending to server: ${serverDateTime}`);

userDateToUTC

Static userDateToUTC(date, format?, timezone): string

Convert a date from a user's timezone to UTC format.

This takes a date in the user's local timezone and converts it to UTC representation using the specified format. Supports the special value '=NOW()' for current date. Use this when submitting date data to the server, which expects UTC dates.

Parameters

NameTypeDefault valueDescription
datestringundefinedA date string (e.g., '1985-10-26') or the special value '=NOW()' for current date.
formatstring'yyyy-mm-dd'A moment.js compatible date format string. Defaults to 'yyyy-mm-dd'. See docs.
timezonestringundefinedThe user's timezone according to the IANA timezone database (e.g., 'Europe/Amsterdam', 'America/New_York').

Returns

string

The date converted to UTC and formatted according to the specified format string.

Example

// Convert user date to UTC
const userDate = '2024-11-27';
const utcDate = eLabSDK2.Date.userDateToUTC(
  userDate,
  'YYYY-MM-DD',
  'Europe/Amsterdam'
);
console.log(utcDate); // '2024-11-27'

Example

// Use current date
const currentUTCDate = eLabSDK2.Date.userDateToUTC(
  '=NOW()',
  'YYYY-MM-DD',
  'America/New_York'
);

© 2023 eLabNext