0

I'm working with a PHP Api witch gives me this json:

{
"title": "hemen",
"category": "time",
"start": "2021-09-27T09:22:00+00:00",
"end": "2021-09-27T13:22:00+00:00",
"isAllDay": false,
"calendar": [{"id": 1}],
"body": null
}

The problem is that in my frontend (Vue) I'm convering the start and end dates to datetime but it adds 2 hours and I don't know why or the best option to fix it.

axios.get('/api/schedules.json').then(function (response) {
    let dataOri = response.data;
    
    // convert
    dataOri.forEach(element => {
        element.start = moment(element.start, "YYYY-MM-DDTHH:mm:ssZ").format("YYYY-MM-DD HH:mm:ss");
        console.log(element.start); // prints '2021-09-27 11:22:00' instead of '2021-09-27 09:22:00'

        element.end = moment(element.end, "YYYY-MM-DDTHH:mm:ssZ").format("YYYY-MM-DD HH:mm:ss");
        console.log(element.end); // prints '2021-09-27 15:22:00' instead of '2021-09-27 13:22:00'
    });
    self.filterSchedules = dataOri;
});
3
  • 1
    It is most likely because of timezones. If you look at the format YYYY-MM-DDTHH:mm:ssZ, you are adding there "Z" in the end. That specifies the time is in UTC, like mentioned here, and you are formatting it into local (without the "Z"). Commented Sep 27, 2021 at 11:27
  • 1
    @Tatranskymedved—the initial times are UTC. The "Z" token in the parse format means "offset as +HH:mm", it doesn't mean UTC (see the moment.js display section). The offset is +00:00 which is effectively UTC. Commented Sep 27, 2021 at 14:34
  • Ah, sorry, you are right, I said it wrongly. Can't edit comment, will update answer to reflect this. Commented Sep 27, 2021 at 14:40

1 Answer 1

1

You are parsing the time with information that "original time is within specified timezone" (+00:00) and then printing it in local time.

There are 2 approaches how to handle the situation, you can either:

  • Ignore these timezones completely (bad approach)
  • Include/parse this timezone information and output it in original UTC time (good approach)

var dateAsStr = "2021-09-27T09:22:00+00:00";

var resultWrong = moment(dateAsStr, "YYYY-MM-DDTHH:mm:ssZ").format("YYYY-MM-DD HH:mm:ss");
var resultBadApproach = moment(dateAsStr, "YYYY-MM-DDTHH:mm:ss").format("YYYY-MM-DD HH:mm:ss");
var resultGoodAproach = moment(dateAsStr, "YYYY-MM-DDTHH:mm:ssZ").utc().format("YYYY-MM-DD HH:mm:ss");

console.log("wrong: " + resultWrong);
console.log("correct (bad approach): " + resultBadApproach);
console.log("correct (good approach): " + resultGoodAproach);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Sign up to request clarification or add additional context in comments.

4 Comments

woooooooo mind blowing! the problem was that I was adding the Z.... grgrggrggrgr
But it is really useful to have it there, if you know that source date is in UTC. If some people open your webpage from UK and from China, you can show them their local time correctly up to their zones. It depends on the usage.
Noooo!! "2021-09-27T09:22:00+00:00" is effectively UTC, it should be parsed with the offset! If the OP wants to see the same date and time, then format it as UTC too.
@RobG Thanks for review! I've updated the answer, feel free to edit/suggest better description.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.