When you use Axios to send the date type to the backend, the time zone is automatically converted, and the time data received by the backend is not correct (the time zone is incorrect, 8 hours earlier).
The submission code is as follows:
<template> <div> </div> </template> <script> export default { data() { return {} }, methods: { test() { let date = new Date() console.log(date) this.axios .post('Values', { date1: date, }) .then((response) => { console.log(response) }) }, }, } </script> <style> </style>
When using Axios to submit data, you can see the request data through the browser’s Network tool, and find that the client’s time is not the same as the time data sent to the back end, which leads to the incorrect time data received by the back end. From observation, we can see that the submission time is eight hours earlier than the client time, that is, the time automatically transferred from East 8 (Beijing, China) to time zone 0 (Greenwich), resulting in the back end receiving time zone 0 as well.
II. Solution:
- The back end uses tolocalTime () to convert ToLocalTime
- Use the Moment.js plugin
<template> <div> </div> </template> <script> export default { data() { return {} }, methods: { test() { let date = this.moment(new Date()).format() console.log(date) this.axios .post('Values', { date1: date, }) .then((response) => { console.log(response) }) }, }, } </script> <style> </style>
If you use the Moment plugin to format the date, you will notice that the time data sent will not be automatically converted to the time zone