0

I'm filtering a table data based on date range(Using vue2-date-range package). Initially the table data is loaded on 'This month' filter.

I've used the following code to get the start and end date of the current month -

  dateRange: {
    startDate: new Date(new Date().getFullYear(), new Date().getMonth(), 1),
    endDate: new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0)
  },

I'm getting the following value as start and end date - "startDate":"2021-03-31T18:30:00.000Z","endDate":"2021-04-29T18:30:00.000Z".

Now in the backend I parse the above dates using Carbon package -

$startDate= Carbon::parse($search->startDate)->format('Y-m-d');
$endDate= Carbon::parse($search->endDate)->format('Y-m-d');

Now the date becomes - startDate - 2021-03-31 & endDate - 2021-04-29. But It should be 2021-04-01 & 2021-04-30 .

Why I'm getting different results.

Ramanath
  • 510
  • 2
  • 12
  • 1
    You are getting the UTC time format, Make sure you insert `2021-04-01` & `2021-04-30` ? please look at your table – STA Apr 06 '21 at 06:17
  • 1
    Seems like a time zone issue. One of these parsers is converting the time value into local time. – Gil Apr 06 '21 at 06:18
  • 1
    Why do you say `2021-03-31T18:30:00.000Z` should result in `2021-04-01` instead of `2021-03-31`? – ikegami Apr 06 '21 at 06:19
  • You're creating local dates in your JavaScript which get converted to their UTC equivalents when serialised into JSON. Also note that when displaying dates, PHP will by default use the server's configured timezone – Phil Apr 06 '21 at 06:21
  • @ikegami just put the above dates in https://timestamp.online/ . You'll see. – Ramanath Apr 06 '21 at 06:21
  • Why? You're not converting to epoch time (1617215400); you're formatting the year (2021), month (3) and day (31) components. Thus, you get 2021-03-31. So again, I ask, why would you expect 2021-03-31T... to produce anything other than 2021-03-31? – ikegami Apr 06 '21 at 06:26
  • You need to pass the timezone you want to consider the picked date server-side, likely the browser timezone: `$startDate= Carbon::parse($search->startDate, $browserTimezone)->format('Y-m-d');` – KyleK Apr 06 '21 at 07:21
  • @EsTeAa— "2021-04-01" will be parsed as UTC too. – RobG Apr 06 '21 at 10:48

0 Answers0