How do I convert certain Date object to the corresponding time in germany?

Parth Savaliya

Let's say I have a Date object like this:

let dateToConvert = new Date(date_string) // represents certain time like 12th Aug 11PM in India

What I want to achieve is to design a function like this:

getGermanDate(dateToConvert): Date {
   // What should be written here?
}

The function should return Date object that is the time in Germany at the time that is in dateToConvert object.

Thanks in advance.

Terry Lennox

You could use native JavaScript functions to convert (toLocaleString), or you could use moment timezone (which is more flexible).

For the toLocaleString call I'm also specifying a Germany date format (by passing "de-DE" to the locale parameter, you could use whichever locale you wish.

function getGermanDate(input) {
    return moment.tz(input, "Europe/Berlin");
}

/* Using moment timezone */
let timestamp = "2020-08-12 23:00:00";
let timeIndia = moment.tz(timestamp, "Asia/Kolkata");
let timeGermany = getGermanDate(timeIndia);
console.log("Time (India):", timeIndia.format("YYYY-MM-DD HH:mm"));
console.log("Time (Germany):", timeGermany .format("YYYY-MM-DD HH:mm"));

/* Using native JavaScript */
let dateToConvert = new Date("2020-08-12T23:00:00+0530");

console.log("Time (India, native):", dateToConvert.toLocaleString('en-IN', { timeZone: 'Asia/Kolkata' }));
console.log("Time (Germany, native):", dateToConvert.toLocaleString('de-DE', { timeZone: 'Europe/Berlin' }));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.25/moment-timezone-with-data-10-year-range.js"></script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert a java.sql.Date object into a GregorianCalendar?

How do I convert an integer to the corresponding words in objective-c?

How do I pass the value of an input type time to a Date object?

How do I create a moment object with just the time and not the date?

How to convert date time format to date and time object?

How do I convert a `DateTime` to the corresponding date and time in Berlin?

How do i convert List into datetime.date object?

How do you convert a Time Range (19:00-20:00) to a TimeStamp/Date Time object?

How do I convert a String of date time data to a LocalDate?

How do I convert this date in to a valid date object?

How do I convert letters to their corresponding number value?

How to convert a string into a date-time object

How do I convert an object containing an Oracle Date to a DateTime?

How do I convert a string into a Time object?

How do I specify a time given a date object?

how do I compare first object of date time to another object of date time in php

How do I convert a character field to date/time format in R?

How do I use mogrify convert only on files with a certain creation date?

How do I run cron-like job at certain date time in the future using Agenda in Nodejs

How to convert a time string to Date object on Javascript?

how do i convert data table date to a certain string format

How I can convert publishedAt to date and time

How do I convert gps time to date in BigQuery

How to get a date time object in UTC that is the last time it was a certain time

How do I make a platform go down after a certain amount of time not colliding with the player object in Unity?

How do I flag date time index for a certain set of time in pandas?

How do I convert unusual time string into date time

I have a time and a time zone - no date. How do I convert that time to another time zone?

How do I convert 'hhmmssss', as a number, to date or time format?