1

I am currently trying to force my application to use UTC time no matter what the system time is. I have a method to create a date:

  public static createDate(date: Date = new Date()): Date {
    return new Date(
      Date.UTC(
        date.getUTCFullYear(),
        date.getUTCMonth(),
        date.getUTCDay(),
        date.getUTCHours(),
        date.getUTCMinutes(),
        date.getUTCSeconds(),
        date.getUTCMilliseconds()
      )
    );
  }

However, I get a time that is 8 hours ahead when it lands in the database. I am not sure what is going on here.

I have tried using other solutions on stack overflow, using libraries like moment and date-fns but i have not had any luck.

4
  • Which database are you using? Is there any default timezone of database you are using? Commented Jun 9, 2021 at 20:04
  • Can you share what you are trying to save into the database, what is returned from the database, how you are using that date value, and why you think it's 8 hours ahead? Commented Jun 9, 2021 at 20:43
  • I am currently using MySql and the Prisma ORM. Prisma asks for a Date when it stores datetime, Commented Jun 9, 2021 at 20:53
  • 1
    Hi. You have clearly fallen into the trap that we call an "XY Problem". Instead of asking about why time zone conversion is happening when you save to your database using Prisma (your actual problem "X"), you've erroneously concluded that the issue is about the Date object (your solution "Y"), and now you're asking about problems with Y. Please read more about the XY problem. Then I suggest opening a new question about the DB parts. Tag it with prisma, typescript and date. Commented Jun 9, 2021 at 21:18

2 Answers 2

1

The Date object is already in UTC. It always is, because the only value it stores internally is a UTC-based numeric timestamp representing the number of milliseconds since 1970-01-01 00:00:00.000 UTC. You can see that timestamp directly with either .getTime() or .valueOf() or by any mechanism that coerces the Date object to a Number. You can also see a string representation of it with .toISOString().

The approach you demonstrated in your question is simply a long and slow way to copy the Date object. You would get the same result with new Date(date).

You said you are having trouble with the time being offset in the database, but you've not shown that part of the code or described anything about your database, so I can't answer that directly. Likely you've got a subtle error in how you pass the Date object into the database that is converting to local time.

Sign up to request clarification or add additional context in comments.

3 Comments

I am using mysql with the Prisma ORM
I have noticed that even with doing ``` return new Date ``` it still returns a date that is 8 hours ahead
You're likely looking at a string representation of that Date object, as if you called .toString() on it - which converts the timestamp to the local time zone while creating the string. The object itself does not have the local time or time zone in it.
0

I realized that because i had set the time on my computer manually, it was using a different time zone, so it was returning a different time. It seems to be fine now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.