Problems dealing with datetime content type

System Information
  • Strapi Version: 4.3.9
  • Operating System: macOS
  • Database: mySQL
  • Node Version: 16.13.2
  • NPM Version: 8.1.2
  • Yarn Version: 1.22.19

Hi there!

I’m trying to experiment with Strapi+Next.js, but one of the problems I’m dealing with Strapi is the Time content type.

The thing is that the data that is returned when I GET it, is like this:

" Time: “12:45:00.000” "

I don’t understand what kind of format is this. Because if I try to do this: new Date(“12:45:00.000”), it returns Invalid Date.

I want this data to be able to be formated with the the JS Date object and with moment.js library.

Am I doing something wrong?

Thanks!

I don’t use moment I find date-fns a lot better. and smaller in size.
What are you trying to convert it to?

Hey there, thanks for the reply!

Again, even if I try with date-fns library, the result is the same (RangeError: Invalid time value)

I’m trying to convert it to a HH:MM format.
I am really confused about the format returned from Strapi though.

So if i get this correct, you are getting BACK from strapi
12:00 as a string or are you sending a datestamp TO strapi and you need to convert it to HH:MM so Tue Dec 06 2022 20:32:34 GMT+0000 (Greenwich Mean Time) to 20:32

What I get from Strapi is: 12:45:00.000
This format cannot be used in the native JS Date or any datetime library.

Kindly reminder :slight_smile:

Maybe something like this would work.

function toDateWithOutTimeZone(date: string): Date {
  let tempTime = date.split(":");
  let dt = new Date();
  dt.setHours(tempTime[0]);
  dt.setMinutes(tempTime[1]);
  dt.setSeconds(tempTime[2]);
  return dt;
}

Thanks for the answer!

I will proceed with this since it’s a side project, but I’m very disappointed by Strapi for this.
Time object is important in projects and to have this kind of format that you can’t do anything seems very odd to me.

Set it as a date time and you don’t get the issue then.
You can select in “date” you can select “Date” “datetime” “time”
Date and time singular are just extracted strings.
Datetime is a datetime object so might be easier work with that.

Hi! Just a small remark. Since time lacks day and TZ, dt should be created with:

let dt = new Date(0)

or you might have problems comparing times