moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.04k stars 729 forks source link

TypeError: input.match is not a function #1440

Open mirajehossain opened 1 year ago

mirajehossain commented 1 year ago

Describe the bug I can't convert the date from one zone to another timezone. getting error

/Users/mirajehossain/Projects/cheq/cheq-marlins-report-generator/node_modules/luxon/build/node/luxon.js:4798
  const matches = input.match(regex);
                        ^

TypeError: input.match is not a function
    at match (/Users/mirajehossain/Projects/cheq/cheq-marlins-report-generator/node_modules/luxon/build/node/luxon.js:4798:25)

To Reproduce Getting Error input.match is not a function when I run this code

const getConvertedStartAndEndDate = ({startDate, endDate, dateFormat, timeZone}) => {
  console.log({startDate, endDate, timeZone});
  const startDateTime = luxon.DateTime.fromFormat(startDate, dateFormat, {
    zone: timeZone,
  }).toUTC();
  const endDateTime = luxon.DateTime.fromFormat(endDate, dateFormat, {
    zone: timeZone,
  })
      .endOf('day')
      .toUTC();
  const formattedStartDate = startDateTime.toFormat('yyyy-MM-dd HH:mm:ss');
  const formattedEndDate = endDateTime.toFormat('yyyy-MM-dd HH:mm:ss');
  return {startDate: formattedStartDate, endDate: formattedEndDate};
};

Actual vs Expected behavior A clear and concise description of what you expected to happen.

Desktop (please complete the following information):

icambron commented 1 year ago

Sounds like you're passing in something unexpected. You're going to have to tell me what {startDate, endDate, dateFormat, timeZone} are, and especially their types.

mirajehossain commented 1 year ago

I got the issue, while I pass the startDate and endDate parameters in this getConvertedStartAndEndDate function, the date format does not match with dateFormat params. That's why the error occurs. Now my suggestion is if we are trying to convert the wrong format/value, the luxon function should return a valid exception rather than throwing this type of error. @icambron

icambron commented 1 year ago

It would be helpful here if you could give me a fully working example

mirajehossain commented 1 year ago

Well, This is the actual function which I have used


const getConvertedStartAndEndDate = ({startDate, endDate, dateFormat, timeZone}) => {
  console.log({startDate, endDate, timeZone});
  const startDateTime = luxon.DateTime.fromFormat(startDate, dateFormat, {
    zone: timeZone,
  }).toUTC();
  const endDateTime = luxon.DateTime.fromFormat(endDate, dateFormat, {
    zone: timeZone,
  })
      .endOf('day')
      .toUTC();
  const formattedStartDate = startDateTime.toFormat('yyyy-MM-dd HH:mm:ss');
  const formattedEndDate = endDateTime.toFormat('yyyy-MM-dd HH:mm:ss');
  return {startDate: formattedStartDate, endDate: formattedEndDate};
};

And I got the error if I call this function like this

const date = new Date();
const { startDate, endDate } = getConvertedStartAndEndDate({
    startDate: date,
    endDate: date,
    dateFormat: 'M/d/yyyy',
    timeZone: timeZone,
  });

And I face this issue because I did not send the date with a specific format The issue is resolved if I call this function like this:

const date = new Date();
const { startDate, endDate } = getConvertedStartAndEndDate({
    startDate: date.toLocaleDateString(),
    endDate: date.toLocaleDateString(),
    dateFormat: 'M/d/yyyy',
    timeZone: timeZone,
  });

The expected scenario is if I sent the wrong parameter it should return a valid error which will be useful to catch the issue.

cc: @icambron

simplecommerce commented 4 months ago

Well, This is the actual function which I have used

const getConvertedStartAndEndDate = ({startDate, endDate, dateFormat, timeZone}) => {
  console.log({startDate, endDate, timeZone});
  const startDateTime = luxon.DateTime.fromFormat(startDate, dateFormat, {
    zone: timeZone,
  }).toUTC();
  const endDateTime = luxon.DateTime.fromFormat(endDate, dateFormat, {
    zone: timeZone,
  })
      .endOf('day')
      .toUTC();
  const formattedStartDate = startDateTime.toFormat('yyyy-MM-dd HH:mm:ss');
  const formattedEndDate = endDateTime.toFormat('yyyy-MM-dd HH:mm:ss');
  return {startDate: formattedStartDate, endDate: formattedEndDate};
};

And I got the error if I call this function like this

const date = new Date();
const { startDate, endDate } = getConvertedStartAndEndDate({
    startDate: date,
    endDate: date,
    dateFormat: 'M/d/yyyy',
    timeZone: timeZone,
  });

And I face this issue because I did not send the date with a specific format The issue is resolved if I call this function like this:

const date = new Date();
const { startDate, endDate } = getConvertedStartAndEndDate({
    startDate: date.toLocaleDateString(),
    endDate: date.toLocaleDateString(),
    dateFormat: 'M/d/yyyy',
    timeZone: timeZone,
  });

The expected scenario is if I sent the wrong parameter it should return a valid error which will be useful to catch the issue.

cc: @icambron

I just got the same error on my end. The issue is the value being passed to the fromFormat function. In my case, it was a string but it had characters that requires to be escaped. So I tested it by using escapeRegExp from lodash and it works.

UPDATE: nevermind, my value wasn't a string, that was my real underlying issue.