Open mirajehossain opened 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.
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
It would be helpful here if you could give me a fully working example
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
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.
Describe the bug I can't convert the date from one zone to another timezone. getting error
To Reproduce Getting Error input.match is not a function when I run this code
Actual vs Expected behavior A clear and concise description of what you expected to happen.
Desktop (please complete the following information):