Moment.js calander method gives incorrect time when converting timestamp

Viewed 127

I am obtaining records from SQL Server database which contain time stamp column which looks like this:

"2020-08-15T22:57:19.000Z"

I am using the moment.js library to obtain a user friendly date value. But when I do:

moment( "2020-08-15T22:57:19.000Z").calendar()

I am getting

Today at 6:57 PM

instead of

Today at 10:57 PM
1 Answers

You can simply use .utc() function and .format() function of momentJs to get your desired results. You do not need to use calender()

Live Demo:

let dbData = '2020-08-15T22:57:19.000Z'
let getTime = moment(dbData).utc().format('hh:mm A') 
console.log('Today at ' + getTime) //10:57 PM
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Related