0

I have a form in which the user selects a date, time and a timezone.

Example:

let date = '02.09.2020';
let time = '16.00';
let timezone = '-07.00';

I want to convert this date with timezone : '+02.00';

I have a hidden text input in which i pass the actualized date format i want.

I don't want to use momment.js for such a small thing.

razvanF
  • 33
  • 1
  • 5
  • What date? Where is your date object? – Mr. Polywhirl Sep 08 '20 at 17:05
  • In what format date will be ? DD:MM:YYYY , MM:DD:YYYY ? any other? – Omkar76 Sep 08 '20 at 17:08
  • The user selects the date, time and a timezone when he wants to have a meeting. When submitted the form I get all that data and want to convert them in my country GMT which is +02.00, and store it in my database. The format i want to achieve is DD:MM:YY – razvanF Sep 08 '20 at 17:16
  • Please put additional information (such as the format of the date) in the OP. – RobG Sep 08 '20 at 22:03

2 Answers2

0

You could construct a UTC date with an offset of the given timezone -07.00. Then you can add the desired offset of +02.00 to that.

Update: I am going to assume the following, based on other user's input.

  • Local date: 2020-02-09 16.00 -07:00
  • UTC date: 2020-02-09 23:00 (subtract the local timezone offset)
  • UTC +2: 2020-02-10 01:00 (add the desired offset)

const offsetDate = (dateStr, timeStr, srcOffset, destOffset) => {
  const tokenize = str => str.split('.').map(v => parseInt(v, 10));
  const [ month, date, year ] = tokenize(dateStr);
  const [ hour, minute ] = tokenize(timeStr);
  const [ srcOffHours, srcOffMins ] = tokenize(srcOffset);
  const [ destOffHours, destOffMins ] = tokenize(destOffset);
  const utcDate = new Date(Date.UTC(year, month - 1, date, hour, minute));
  const srcOff = srcOffHours * 36e5 + srcOffMins * 6e4;
  const destOff = destOffHours * 36e5 + destOffMins * 6e4;
  utcDate.setTime(utcDate.getTime() - srcOff + destOff);
  console.log(`Time: ${utcDate.toISOString()}`);
  return [
    utcDate.getUTCDate(),
    utcDate.getUTCMonth() + 1,
    utcDate.getUTCFullYear().toString().substr(2)
  ].map(v => (v + '').padStart(2, '0')).join(':');
};

const date = '02.09.2020';
const time = '16.00';
const timezone = '-07.00';

console.log(offsetDate(date, time, timezone, '+02.00')); // 10:02:20
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • Very nice. I think here ""utcDate.setTime(utcDate.getTime() + srcOff + destOff)"", it should be decrease srcOff not add. But unfortunately it works for some countries and for others not. I don't get it. Same issue like my code. – razvanF Sep 08 '20 at 17:43
  • @razvanF You always add. If offset is negative, you will subtract because the operation flips. – Mr. Polywhirl Sep 08 '20 at 17:57
  • This has a number of issues. The OP format is d.m.y, but the *offsetDate* function assumes m.d.y. It also incorrectly applies the offset: if the initial time is 16:00 in -7, then the UTC time is 23:00 and in +2 it is 01:00 the following day (i.e. 9 hours later). It probably also doesn't correctly calculate negative offsets with minute components like -2:30 (America/St_Johns). – RobG Sep 08 '20 at 22:06
  • @RobG OP did not tell us what the expected output is, but I could adjust the logic to handle the given date as the local date. – Mr. Polywhirl Sep 08 '20 at 22:10
-1

You can do something like this

let date = new Date();
let event = date.toLocaleString('ko-KR', { timeZone: 'UTC' })) // if the timezone was Korea