wong van
wong van

Reputation: 11

convert json object (php date) with javascript

I have the following json object date that comes from php, and I need to display date with javascript in user timezone in dd/mm/YYYY HH:mn format:

{ "date": "2018-05-01 22:06:01.000000", 
  "timezone_type": 3, 
  "timezone": "Europe/Berlin" 
} 

Upvotes: 1

Views: 86

Answers (1)

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22875

You can use moment.js an excellent library for handling dates

https://momentjs.com/

https://momentjs.com/timezone/

let json = { "date": "2018-05-01 22:06:01.000000", 
  "timezone_type": 3, 
  "timezone": "Europe/Berlin" 
};

let date =  moment.tz(json.date, json.timezone);

console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.17/moment-timezone-with-data.min.js"></script>

Upvotes: 2

Related Questions