moment().fromNow() ignores UTC

I’ve seen multiple questions around this and this post did not clarify it for me.

I get ISO 8601 time from a query and want to display that result different depending on user config in a datagrid.

I have tried different methods both from documentation and stackoverflow, but none seems to give the expected result, which would be UTC-time fromNow(), but instead shows local time fromNow(). In console the values seems right until moment.fromNow()

                    // Changes value on item["StartTime"] based on user config before it pushed. 
                else {
                    let time = item["StartTime"] // Outputs ISO 8601
                    

                    switch (true) {
                        case (userConfig.general.dateFormat == "From now" && userConfig.general.useLocal == false):
                            var utcDate = new Date(time).toUTCString();                              
                            utcDate = moment(utcDate).fromNow()
                            item["StartTime"] = utcDate
                            break;
                            ....
                            ....

                }

4 thoughts on “moment().fromNow() ignores UTC”

  1. You should use moment.utc.

    From the docs:

    By default, moment parses and displays in local time.
    If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().

    Reply

Leave a Comment