Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing MySQL date with moment

Tags:

mysql

momentjs

I'm getting a date from MySQL and trying to parse it with moment

let date = moment('2017-02-27T00:00:00.000Z').format('YYYY-MM-DD')
console.log(date) //2017-02-26

Why am i losing a day; And what is the correct way to parse it?

like image 615
LeagueOfJava Avatar asked Oct 16 '25 14:10

LeagueOfJava


1 Answers

This is happening because of Time Zone.

You can change it using .tz() to use specific timezone or use .utc()

let date = moment('2017-02-27T00:00:00.000Z').utc().format('YYYY-MM-DD')
console.log(date) //2017-02-27

http://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/

like image 162
Nielsen Martins Gonçalves Avatar answered Oct 18 '25 04:10

Nielsen Martins Gonçalves