Search code examples
javascriptnode.jstimezoneutcluxon

Converting UTC to country local time


I'm trying to convert the UTC time that I've in my database to a local country specific time. I'm looking at different libraries: luxon Day.js etc. I want to get the local time in Germany if I've the UTC time. I've the user's country in my records. If UTC time is 10am then local time in Germany should be 11am. This is what I've tried in order to test the luxon library:

const { DateTime } = require("luxon");

const date = new Date(Date.UTC(2020, 12, 9, 10, 0, 0));
let ge_date = DateTime.fromJSDate(date, { locale: "ge" });
ge_date.setZone("Europe/Berlin");
console.log(ge_date);

but it doesn't seem to be working.


Solution

  • setZone() returns a newly constructed DateTime but it doesn't change your original variable. The best solution would be to chain the .setZone() after your .fromJSDate() function.

    let ge_date = DateTime.fromJSDate(date, { locale: "ge" }).setZone("Europe/Berlin");
    console.log(ge_date.toString());