React-dates: Displaying UTC version of date

Created on 15 Feb 2018  路  4Comments  路  Source: airbnb/react-dates

Hi! So I've got react-dates as a component picking dates and it's all dandy. Before I send it to the server, I convert to the utc version of the ISO String, going to the start of the day first (I wanted to emphasize that I really only care about the date, not the time). That looks like this:

startDate
  .utc()
  .startOf('day')
  .toISOString()

endDate
  .utc()
  .startOf('day')
  .toISOString()

However, when the server gives back a response and we update the UI, the date shows up as the date before. I know why this is -- I'm on the East coast of the US, so midnight at UTC is actually a few hours earlier than midnight here. I don't want to change the date object -- I'd just like to display the UTC version of the date. I can't seem to figure out how to do this using only format strings in moment, and I don't really think I should have to use format strings alone, given that I could get the right string if I had the actual moment object and performance is not really a concern here. Also, it's showing up wrong in the calendar, so I don't think it's just a string issue. I basically want the whole component to "pretend" I live in England.

What's the best way to go about doing this? Is such a thing even possible?

Most helpful comment

I'll just leave this here:
supervillain_plan_2x

All 4 comments

My apologies -- this probably won't be very helpful to anyone else, but I figured out that the moment object I was passing in as a prop needed to be set to UTC. I can't get too into detail because I don't really understand it that well myself, but basically calling

const startDate = moment(this.props.startDate).utc();
const endDate = moment(this.props.endDate).utc();

before passing in the dates as props results in the component rendering them as though I were in UTC myself. Solved my problem, anyways.

I'll just leave this here:
supervillain_plan_2x

Would almost be worse if it didn't put any cities into the UTC-9 time zone... but some of the rural areas...

I'm seeing that the date picker still displays local version of date, even if date is in UTC+0

see https://github.com/airbnb/react-dates/issues/1138

Was this page helpful?
0 / 5 - 0 ratings

Related issues

augnustin picture augnustin  路  3Comments

prztrz picture prztrz  路  3Comments

HartiganHM picture HartiganHM  路  3Comments

AsasinCree picture AsasinCree  路  3Comments

krissalvador27 picture krissalvador27  路  3Comments