Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let dateSetting = ((typeof date !== 'string') || (date.indexOf('moment(') === -1)) ? moment(date) : null;
if (dateSetting && dateSetting.isValid()) {
return dateSetting.toDate();
}
dateSetting = null;
try {
const value = Evaluator.evaluator(`return ${date};`, 'moment')(moment);
if (typeof value === 'string') {
dateSetting = moment(value);
}
else if (typeof value.toDate === 'function') {
dateSetting = moment(value.toDate().toUTCString());
}
else if (value instanceof Date) {
dateSetting = moment(value);
}
}
catch (e) {
return null;
}
if (!dateSetting) {
return null;
}
// Ensure this is a date.
if (!dateSetting.isValid()) {
return null;
}
return dateSetting.toDate();
export function formatDate(value, format, timezone) {
const momentDate = moment(value);
if (timezone === currentTimezone()) {
// See if our format contains a "z" timezone character.
if (format.match(/\s(z$|z\s)/)) {
loadZones();
if (moment.zonesLoaded) {
return momentDate.tz(timezone).format(convertFormatToMoment(format));
}
else {
return momentDate.format(convertFormatToMoment(format.replace(/\s(z$|z\s)/, '')));
}
}
// Return the standard format.
return momentDate.format(convertFormatToMoment(format));
}
if (timezone === 'UTC') {
return date.isValid() ? date.toDate() : null;
}
let dateSetting = ((typeof date !== 'string') || (date.indexOf('moment(') === -1)) ? moment(date) : null;
if (dateSetting && dateSetting.isValid()) {
return dateSetting.toDate();
}
dateSetting = null;
try {
const value = Evaluator.evaluator(`return ${date};`, 'moment')(moment);
if (typeof value === 'string') {
dateSetting = moment(value);
}
else if (typeof value.toDate === 'function') {
dateSetting = moment(value.toDate().toUTCString());
}
else if (value instanceof Date) {
dateSetting = moment(value);
}
}
catch (e) {
return null;
}
if (!dateSetting) {
return null;
}
// Ensure this is a date.
if (!dateSetting.isValid()) {
return null;
if (format.match(/\s(z$|z\s)/)) {
loadZones();
if (moment.zonesLoaded) {
return momentDate.tz(timezone).format(convertFormatToMoment(format));
}
else {
return momentDate.format(convertFormatToMoment(format.replace(/\s(z$|z\s)/, '')));
}
}
// Return the standard format.
return momentDate.format(convertFormatToMoment(format));
}
if (timezone === 'UTC') {
const offset = offsetDate(momentDate.toDate(), 'UTC');
return `${moment(offset.date).format(convertFormatToMoment(format))} UTC`;
}
// Load the zones since we need timezone information.
loadZones();
if (moment.zonesLoaded) {
return momentDate.tz(timezone).format(`${convertFormatToMoment(format)} z`);
}
else {
return momentDate.format(convertFormatToMoment(format));
}
}
jsonLogic.add_operation('relativeMaxDate', (relativeMaxDate) => {
return moment().add(relativeMaxDate, 'days').toISOString();
});
export function offsetDate(date, timezone) {
if (timezone === 'UTC') {
return {
date: new Date(date.getTime() + (date.getTimezoneOffset() * 60000)),
abbr: 'UTC'
};
}
const dateMoment = moment(date).tz(timezone);
return {
date: new Date(date.getTime() + ((dateMoment.utcOffset() + date.getTimezoneOffset()) * 60000)),
abbr: dateMoment.format('z')
};
}
jsonLogic.add_operation('getDate', (date) => {
return moment(date).toISOString();
});
export function momentDate(value, format, timezone) {
const momentDate = moment(value);
if (timezone === 'UTC') {
timezone = 'Etc/UTC';
}
if ((timezone !== currentTimezone() || (format && format.match(/\s(z$|z\s)/))) && moment.zonesLoaded) {
return momentDate.tz(timezone);
}
return momentDate;
}
export function getDateSetting(date) {
if (_.isNil(date) || _.isNaN(date) || date === '') {
return null;
}
if (date instanceof Date) {
return date;
}
else if (typeof date.toDate === 'function') {
return date.isValid() ? date.toDate() : null;
}
let dateSetting = ((typeof date !== 'string') || (date.indexOf('moment(') === -1)) ? moment(date) : null;
if (dateSetting && dateSetting.isValid()) {
return dateSetting.toDate();
}
dateSetting = null;
try {
const value = Evaluator.evaluator(`return ${date};`, 'moment')(moment);
if (typeof value === 'string') {
dateSetting = moment(value);
}
else if (typeof value.toDate === 'function') {
dateSetting = moment(value.toDate().toUTCString());
}
else if (value instanceof Date) {
dateSetting = moment(value);
}
jsonLogic.add_operation('relativeMinDate', (relativeMinDate) => {
return moment().subtract(relativeMinDate, 'days').toISOString();
});