diff --git a/packages/p2p-v2/package.json b/packages/p2p-v2/package.json index 547d1617b8d5..ffc7148d529e 100644 --- a/packages/p2p-v2/package.json +++ b/packages/p2p-v2/package.json @@ -16,7 +16,6 @@ "@deriv/api": "^1.0.0", "@deriv/integration": "^1.0.0", "@deriv/react-joyride": "^2.6.2", - "@deriv/utils": "^1.0.0", "@sendbird/chat": "^4.9.7", "clsx": "^2.0.0", "i18next": "^22.4.6", diff --git a/packages/p2p-v2/src/utils/time.ts b/packages/p2p-v2/src/utils/time.ts index b4626cf070a5..b8bfd7f62de7 100644 --- a/packages/p2p-v2/src/utils/time.ts +++ b/packages/p2p-v2/src/utils/time.ts @@ -1,4 +1,28 @@ -import { toMoment } from '@deriv/utils'; +import moment from 'moment'; + +/** + * Function that converts a numerical epoch value into a Moment instance + */ +export const epochToMoment = (epoch: number) => moment.unix(epoch).utc(); + +/** + * Function that takes a primitive type and converts it into a Moment instance + */ +export const toMoment = (value?: moment.MomentInput): moment.Moment => { + if (!value) return moment().utc(); // returns 'now' moment object + if (moment.isMoment(value) && value.isValid() && value.isUTC()) return value; // returns if already a moment object + if (typeof value === 'number') return epochToMoment(value); // returns epochToMoment() if not a date + + if (/invalid/i.test(moment(value).toString())) { + const todayMoment = moment(); + const daysInMonth = todayMoment.utc().daysInMonth(); + const valueAsNumber = moment.utc(value, 'DD MMM YYYY').valueOf() / (1000 * 60 * 60 * 24); + return valueAsNumber > daysInMonth + ? moment.utc(todayMoment.add(value.valueOf(), 'd'), 'DD MMM YYYY') + : moment.utc(value, 'DD MMM YYYY'); // returns target date + } + return moment.utc(value); +}; /** * return the number of days since the date specified