简体   繁体   English

管理时区 JS 两个小时的差异

[英]manage timezones JS difference of two hours

frontend side I send axios this date away:前端我在这个日期发送 axios:

Mon Jun 13 2022 18:29:00 GMT+0200 (Ora legale dell’Europa centrale)

because the backend (laravel 8.x) side saves me the usual one as follows:因为后端(laravel 8.x)端为我节省了通常的一个,如下所示:

2022-06-13 16:29:00

I wish he would save me 18:29 and not 16:29我希望他能在 18:29 而不是 16:29 救我

how can i handle this?我该如何处理? i can't install libraries like moment.js我无法安装像 moment.js 这样的库

This will likely be due to your app timezone being set to UTC.这可能是由于您的应用时区设置为 UTC。 I personally would recommend storing everything in UTC and converting that for display purposes on the front end.我个人建议将所有内容存储在 UTC 中,并将其转换为前端显示的目的。

However, if you must store them in the same format, you'll need to set the timezone in config/app.php但是,如果您必须以相同的格式存储它们,则需要在config/app.php设置时区

/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/

'timezone' => 'UTC', <--- Change this to your timezone eg: Africa/Cairo

Select the correct timezone from this list. 从此列表中选择正确的时区。

GMT+0200 可用的时区

Don't forget to run php artisan config:clear to ensure your timezone configuration changes are being used.不要忘记运行php artisan config:clear以确保正在使用您的时区配置更改。

I solved as follows:我解决如下:

    return new Date(year, month - 1, day, hours, minutes, seconds).toString().slice(0, 24);

can you do better?你能做得更好吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM