question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Date Selected is One Day Off

See original GitHub issue

Hi, I’m having an interesting issue with my usage of the datepicker component.

My code is pretty basic, it looks like this:

<ReactDatePicker
  onChange={(val) => this.handleChange(val)}
  selected={this.state.currentDate}
/>

and my handler fn

  handleChange(momentDate) {
    console.log(momentDate.toISOString())
    this.setState({currentDate: momentDate});
  }

The issue that I see is that when I select a date from the dropdown, the visual aspects of the component appear to be working fine – I select 09/10 (sept 10th) and that’s what I see in the input field.

However, in my console.log() in my handler function, I see that when I select 09/10, the output is 2017-09-09T21:00:00.000Z. momentDate.toISOString() is ultimately what I’m sending to the DB to be saved, so it’s problematic that it’s one day off.

I’m not sure what to make of this. I was expecting to see 2017-09-10T21:00:00.000Z. It looks like momentDate is localized, so if I’m in a UTC + 3 timezone and choose 09/10 00:00:00, I’m actually choosing 09/09 21:00:00 (my hypothesis). Even if I set the utcOffset prop to 0 it looks like the localization is still happening.

Am I doing something wrong? Is there a way for me to just kind of…turn off the localization? I’m using the component purely to just pick a day in the calendar. I don’t care what timezone they’re in, if they pick 09/10, they pick 09/10.

Thanks!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:35
  • Comments:68 (1 by maintainers)

github_iconTop GitHub Comments

40reactions
FatehAKcommented, Jun 22, 2020

This works: const offsetDate = new Date(selected.getTime() - (selected.getTimezoneOffset() * 60000));

Just put it in onChange() handler

15reactions
nwkeeleycommented, Sep 22, 2017

Also experiencing this issue.

I open it up, select date its off by 1 day. I then open it up and select any other subsequent day and its correct.

Read more comments on GitHub >

github_iconTop Results From Across the Web

datepicker date off by one day - Stack Overflow
The reason you're off by day is because UTC time is for a timezone that is ahead of yours. (Since you didn't also...
Read more >
Solved: DatePicker is one day off - Power Platform Community
It shows data one day behind. For example if Today() is 23.09.2020 - my gallery will show date up to 22.09.2020. When I...
Read more >
A Complete Guide to JavaScript Dates (and why your date is ...
A Complete Guide to JavaScript Dates (and why your date is off by 1 day). #javascript. JavaScript dates are confusing.
Read more >
Why the JavaScript date is one day off? - Sunzala Technology
The JavaScript date object is one day off because of timezone offset. Adding the timzezone difference gives a correct date.
Read more >
Why is does the date selector show previous date? - Forums
I use a date time picker in my coach. When I select a date and then convert it to a string, it displays...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found