0

I've been looking for someone with the same problem here but I could not find. Whenever I call Date() on my hidden value it seems to be subtracting one day. I am using Chrome.

I am passing a hidden value to my html:

<input type="hidden" name="start_date" value="2018-07-29" class="start-date" id="id_start_date">

It's correct when I call:

var hiddenDate =  $('#id_start_date')[0].value;
alert(hiddenDate);   # 2018-07-29

But incorrect if I call:

var date = new Date(hiddenDate);
alert(date); # Sat Jul 28 2018 19:00:00 GMT-0500 (Central Daylight Time)

What am I doing wrong and how can I fix it? Thanks

sptramp
  • 897
  • 2
  • 10
  • 29
  • timezones...... – Daniel A. White Aug 03 '18 at 18:21
  • 2
    It was parsed as UTC (the time is 5 hours behind in your UTC-0500 timezone). The right way to deal with that depends on what you end up doing with the date object. So: where does `date` go? – Ry- Aug 03 '18 at 18:21
  • I'd convert to timestamp instead of java's `Date` object. – Dan Aug 03 '18 at 18:22
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC – pavi2410 Aug 03 '18 at 18:25
  • @Ry- I increment that value to set up days for an entire week. I've tried the suggested solutions in this post but had no success. – sptramp Aug 03 '18 at 19:15
  • Try using the UTC equivalents of functions on the date object you already have, then, like `getUTCDate()` instead of `getDate()`. (Dates are really hard to get right, though.) – Ry- Aug 03 '18 at 21:20

0 Answers0