I am using the date-fns
library to get the months between a couple of date values using the eachMonthOfInterval
function, like so:
const startingMonth = "2024-10-01 00:00:00";
const endingMonth = "2024-12-01 00:00:00";
const allMonths: any[] = eachMonthOfInterval(
{
start: startingMonth,
end: endingMonth,
},
{
in: tz("Asia/Calcutta"),
}
);
console.log(allMonths);
console.log(allMonths[0].internal);
I am using the tz
function from the library "@date-fns/tz"
to specify the time zone in which I want the result (in this case, "Asia/Calcutta").
The following is the console.log
that I get:
[
TZDate 2024-09-30T18:30:00.000Z {
timeZone: 'Asia/Calcutta',
internal: 2024-10-01T00:00:00.000Z
},
TZDate 2024-10-31T18:30:00.000Z {
timeZone: 'Asia/Calcutta',
internal: 2024-11-01T00:00:00.000Z
},
TZDate 2024-11-30T18:30:00.000Z {
timeZone: 'Asia/Calcutta',
internal: 2024-12-01T00:00:00.000Z
}
]
2024-10-01T00:00:00.000Z
Insted of any[]
, I tried using the type TZDate
from the "@date-fns/tz"
library, but I get this red squiggly line under the property internal
in the line console.log(allMonths[0].internal)
When I hover on the red squiggly line, it says "Property 'internal' does not exist on type 'TZDate'."
How can I add a type to the constant allMonths
without using the type any
?
internal
is not a property of TZDate
. It only adds in runtime and should not be used in external functions. If you want to convert TZDate
to Date
or formatted string
, use its functions instead of accessing to internal
.
for (const month of allMonths) {
console.log(month.toISOString());
}