Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { DateTime, Interval } from 'luxon';
- const rangeTest: [DateTime, DateTime] = [
- DateTime.fromISO('2022-01-01T00:00:00Z'),
- DateTime.fromISO('2022-05-01T00:00:00Z'),
- ];
- function normalizeRange(range: [DateTime, DateTime]): [DateTime, DateTime] {
- return [
- range[0]?.isValid ? range[0] : DateTime.fromMillis(-864 * Math.pow(10, 12)),
- range[1]?.isValid ? range[1] : DateTime.fromMillis(864 * Math.pow(10, 12)),
- ];
- }
- function checkDate(
- range: [DateTime, DateTime],
- targetDate: DateTime,
- fallbackDate: DateTime
- ): DateTime {
- if (!fallbackDate) {
- return null;
- }
- const normalizedRange = normalizeRange(range);
- const contantainsTarget = Interval.fromDateTimes(
- normalizedRange[0],
- normalizedRange[1]
- ).contains(targetDate);
- return contantainsTarget ? targetDate : fallbackDate;
- }
- const boxOffice = {
- vFrom: null,
- vTo: null,
- //vTo: DateTime.fromISO('2022-05-01T00:00:00Z'),
- };
- const ticket = {
- vFrom: DateTime.fromISO('2022-05-01T00:00:00Z'),
- };
- const vRange: [DateTime, DateTime] = [boxOffice.vFrom, boxOffice.vTo];
- const normalized = {
- vFrom: checkDate(vRange, ticket.vFrom, boxOffice.vFrom),
- };
- console.log(normalized);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement