0

I am building an browser-based application where the user sets the timezone of their application, e.g. "America/New_York". I have a part of the application that allows users to set reminders and I want the user to specify that they want, for example, to set a reminder for 28th Jan 2020 at 9am in their timezone. This should work such that it they are on holiday in London and used the application to set this reminder, displaying this reminder would still always be in the New York timezone. I store the dates as UTC in the applications database and when converting this back show the user will display it according to their timezone. I have tried this method in Codesandbox, to convert the date to UTC to store in the database:

const dateInNewYork = dayjs("2020-01-28 09:00:00", { timeZone: "America/New_York" }).toISOString();
// displays as 2020-03-29T11:00:00.000Z - shouldn't this be 2020-01-28T14:00:00.000Z ?

What am I doing wrong here?

JoeTidee
  • 24,754
  • 25
  • 104
  • 149

1 Answers1

0
let timeSt = dayjs(time).unix()* 1000;
timeSt += 60*60*1000*8 //8 is means china beijing time.
let whatYouNeed = dayjs(timeSt).format("YYYY-MM-DD HH:mm");
XW L
  • 11
  • 2
  • 2
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – ysf Jun 24 '20 at 08:23