1

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.

Lexx
  • 705
  • 2
  • 9
  • 19
  • 1
    You may be interested in this answer: https://stackoverflow.com/a/47808440/11283219. This solution gives you ability to use native `.toLocaleDateString()` – Maksymilian Tomczyk Dec 09 '20 at 11:01
  • 1
    // this should work as a built in feature // get local date and time `const dt = new Date().toLocaleString();` // get local date only `const d = new Date().toLocaleDateString();` // get local time only `const t = new Date().toLocaleTimeString();` just put your date inside the `new Date(hereYourDBdate)` – TradeCoder Dec 09 '20 at 11:19

1 Answers1

3

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());
jndnzl
  • 146
  • 1
  • 5