contentful.js icon indicating copy to clipboard operation
contentful.js copied to clipboard

Issue with format of type EntryFields.Date

Open venerated opened this issue 4 months ago • 0 comments

I don't think this is necessarily a bug, but I have hit a dead-end trying to work around it, and searching has come up empty.

From node_modules/contentful/dist/types/types/entry.d.ts:

type Date = `${number}-${number}-${number}T${number}:${number}:${number}Z`

so when trying to make a content API call with parameters like...

{
        content_type: 'event',
        include: 10,
        locale: locale.value,
        limit: 500,
        'fields.startDatetime[gte]': new Date()
  }

there is a TypeScript error, since the date is formatted incorrectly. This is of course a simplified version, the date is set by the user.

Where I am running into an issue is that there is no good way to format a date to work with this format.

Even if I create a helper function that returns the individual values formatted in the string Contentful expects, the individual numbers become an issue if they're less than 10, because I can't prepend a 0, since it's a number. So for example, if the day or month are less than 10, I end up with a string that looks like this 1-1-2024T11:59:59 which does not work when sending the request to Contentful, the date needs to be formatted like 01-01-2024T11:59:59.

Thank you in advanced for any insight or help.

venerated avatar Oct 04 '24 20:10 venerated