Erick Gallani Erick Gallani - 2 months ago 11
AngularJS Question

How to get the correct datetime from momentjs

I'm facing some difficulties to get the right datetime using the momentjs library.

I'm doing this

****--------- Datetime ----------****

Timestamp received from database -> 2016-07-12 17:21:40 <- THIS IS A UTC DATE TIME

moment.tz(datetime, moment.tz.guess()); //datetime value is '2016-07-12 17:21:40' and moment.tz.guess() is returning "America/Sao_Paulo"

// inspect the moment object returned
q{ _isAMomentObject:true, _i:"2016-07-12 17:21:40", _f:"YYYY-MM-DD HH:mm:ss", _isUTC:true, _pf:Object…}
_a: Array[7]
_d:Tue Jul 12 2016 14:21:40GMT-0300 (BRT) <- THIS IS THE CORRECT DATE TIME THAT I WANT
_f:"YYYY-MM-DD HH:mm:ss"
_i:"2016-07-12 17:21:40"
_isAMomentObject:true
_isUTC:true
_isValid:true
_locale:B
_offset:-180
_pf:Object
_z:h
__proto__:Object

// moment object formatted .format('YYYY-MM-DD HH:mm:ss')
2016-07-12 17:21:40 <- THIS IS THE DATE TIME THAT I'M GETTING, AND IT IS WRONG

****-------------------****


I don't know what I'm doing wrong here.

Answer

Solution:

I found out how to archive this.

moment.tz(moment.utc(datetime), moment.tz.guess());
Comments