I need to convert German local time records, without zone info, i.e.,
var data = [
"2017-12-02 20:27:02",
"2017-12-02 20:50:00"
]
getTimezoneOffset
toISOString
var d = new Date(data[0])
> Sat Dec 02 2017 20:27:02 GMT+0100 (CET)
d.toISOString()
> '2017-12-02T19:27:02.000Z'
You can use moment.js
to make date calculations in JavaScript. To parse a date in a specific timezone, you also need moment.js timezone
. You can then parse the date with your format in the tz()
function. The date is then available as a moment
object, with which you can use all moment.js
features (e.g. time/date calculations, formatting, etc.). See the example below:
var dateString = '2017-12-02 20:27:02';
var dateFormat = 'YYYY-MM-DD HH:mm:ss';
var date = moment.tz(dateString, dateFormat, "Europe/Berlin");
console.log(date.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone-with-data.min.js"></script>