Advertisement
attilan

Untitled

Mar 24th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. import { DateTime, Interval } from 'luxon';
  2.  
  3. const rangeTest: [DateTime, DateTime] = [
  4. DateTime.fromISO('2022-01-01T00:00:00Z'),
  5. DateTime.fromISO('2022-05-01T00:00:00Z'),
  6. ];
  7.  
  8. function normalizeRange(range: [DateTime, DateTime]): [DateTime, DateTime] {
  9. return [
  10. range[0]?.isValid ? range[0] : DateTime.fromMillis(-864 * Math.pow(10, 12)),
  11. range[1]?.isValid ? range[1] : DateTime.fromMillis(864 * Math.pow(10, 12)),
  12. ];
  13. }
  14.  
  15. function checkDate(
  16. range: [DateTime, DateTime],
  17. targetDate: DateTime,
  18. fallbackDate: DateTime
  19. ): DateTime {
  20. if (!fallbackDate) {
  21. return null;
  22. }
  23.  
  24. const normalizedRange = normalizeRange(range);
  25.  
  26. const contantainsTarget = Interval.fromDateTimes(
  27. normalizedRange[0],
  28. normalizedRange[1]
  29. ).contains(targetDate);
  30.  
  31. return contantainsTarget ? targetDate : fallbackDate;
  32. }
  33.  
  34. const boxOffice = {
  35. vFrom: null,
  36. vTo: null,
  37. //vTo: DateTime.fromISO('2022-05-01T00:00:00Z'),
  38. };
  39.  
  40. const ticket = {
  41. vFrom: DateTime.fromISO('2022-05-01T00:00:00Z'),
  42. };
  43.  
  44. const vRange: [DateTime, DateTime] = [boxOffice.vFrom, boxOffice.vTo];
  45.  
  46. const normalized = {
  47. vFrom: checkDate(vRange, ticket.vFrom, boxOffice.vFrom),
  48. };
  49.  
  50. console.log(normalized);
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement