Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular http method changing the hour in datetime

I have the weirdest bug ever. I am talking to my API via angularjs and I try to update something by doing the following

  console.log('updated: ', event.startsAt);
  $http({
    method: 'PUT',
    url: baseurl + "/event/" +  event.id,
    data: {title: event.title, startsAt: moment(event.startsAt).clone().toDate(), entriesAllowed: event.entriesAllowed},
    headers: {'Content-type': 'application/json'},
    handleError: true
  })

So the problem is in the event.startsAt variable. In the console log it give me the correct time:

Sat Dec 24 2016 16:15:00 GMT+0100 (CET)

but as soon as I send it and access it with my api it gives the same datetime but minus 1 hours so

Sat Dec 24 2016 15:15:00 GMT+0100 (CET)

Any idea what this could be? I use laravel for my api and I am getting the datetime with

$event->startsAt = Carbon::parse($request->get('startsAt'), 'Europe/Amsterdam');

Things I have tried myself

  • Use momentJS to change datetime to a different format.
  • adding/removing timezone in Laravel.
like image 791
Reshad Avatar asked Apr 26 '26 11:04

Reshad


1 Answers

Assuming event.startsAt is a Date object, then you should probably serialize it to ISO8601 format first.

Try event.startsAt.toISOString() first. That will also convert to UTC, but you should be able to handle that easily on the server.

If for some reason you need the original time zone offset persisted, moment can help with that using moment(event.startsAt).format()

like image 60
Matt Johnson-Pint Avatar answered Apr 28 '26 18:04

Matt Johnson-Pint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!