0

I'm obtaining data.value which is a time in the format: hh:mm a - for example 12:30 am.

I also know:

  1. the local timezone of the user (userTimeZone)
  2. the timezone of the venue (venueTimeZone)

I need to convert the time selected by the user (data.value) to the correct date in the venueTimeZone. For example, if the user is in Americas/New York and they selected 1:30PM on the 20/05/2022, and the venue is in Americas/Los Angeles - the value I am interested in obtaining is 20/05/2022 10:30AM.

This is my attempt, however the timezone itself doesn't change - I think this is because when I create the userDateTime with moment I don't specify a time offset, but I'm not sure how to obtain the offset from userTimeZone, whilst accounting for DST.

            const userTimeZone = _.get(
                Intl.DateTimeFormat().resolvedOptions(),
                ['timeZone']
            );

            const venueDateStr = new Date().toLocaleString('en-US', {
                timeZone: venueTimeZone,
            });

            const Date = new Date(restaurantDateStr);
            const venueYear = venueDate.getFullYear();
            const venueMonth = `0${venueDate.getMonth() + 1}`.slice(-2);
            const venueDateOfMonth = `0${venueDate.getDate()}`.slice(-2);

            const userDateTime = createDateAsUTC(
                moment(
                    `${venueDateOfMonth}/${venueMonth}/${venueYear} ${data.value}`,
                    'DD/MM/YYYY hh:mm a'
                ).toDate()
            ).toLocaleString('en-US', { timeZone: venueTimeZone });

EDIT - I do not have the city offset, I have the timezone name, therefore I cannot use any suggested answer which relies on city offset.

Alk
  • 5,215
  • 8
  • 47
  • 116
  • Does this answer your question? [Convert date to another timezone in JavaScript](https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – Yogi May 21 '22 at 23:11
  • No it doesn't - this requires the city offset, and as I mentioned in the question, I have the timezone not the city offset. – Alk May 21 '22 at 23:30
  • can you post an example? – Alk May 22 '22 at 00:39
  • I think you're looking for something like this: [*Calculate Timezone offset only for one particular timezone*](https://stackoverflow.com/questions/61361914/calculate-timezone-offset-only-for-one-particular-timezone), where you get a date and time in one location and convert it to the date and time in another location. A library will really help. – RobG May 22 '22 at 03:09
  • What are you calling "city offset" and "timezone"? Timezones used to be pretty simple: the world was zoned more or less on lines of longitude with even increments of 1 hour or sometimes 1:30 offsets. However, IANA introduced the concept of representative locations to accommodate historical changes in offsets, so now "timezone" is really the geographic region that has the same offset history as the nominated IANA representative location. So "Asia/Kolkata" is referred to as a timezone, even though it's actually a location. – RobG May 22 '22 at 03:11
  • If by "timezone" you mean the older longitudinal zones like PST, PDT, etc. then you're out of luck because 1. The names and abbreviations aren't standardised and 2. within those timezones are many different historical changes to offsets and daylight saving that can't be generally applied to the entire zone. Hence the reason for IANA representative locations (which are a pretty clever solution, but should be called something other than "timezone" to avoid confusion). ;-) – RobG May 22 '22 at 03:14
  • @RobG I provided an example in the question - I'm referring to things like America/New York – Alk May 22 '22 at 09:29
  • "*I'm referring to things like America/New York*" as what? Timezone or "city offset"? Timezones like EST, PDT, etc. define a specific offset that doesn't change, they are fixed forever. That's why a place that normally observes EST uses EDT during daylight saving, to indicate the different offset. Just because a place uses EST at some time doesn't mean that it will change to EDT on the current rules for changeover, that depends on the actual date and where the user is located, hence IANA representative locations (which I *think* you are calling "city offset"). – RobG May 23 '22 at 03:08

1 Answers1

1

Consider using Luxon - the successor to Moment. (See Moment's project status.)

// Parse your input string using the user's local time zone
// (this assumes the current local date)
const local = luxon.DateTime.fromFormat('1:30 pm', 'h:mm a');

// Convert to the desired time zone
const converted = local.setZone('America/Los_Angeles');

// Format the output as desired
const formatted = converted.toFormat('dd/MM/yyyy h:mm a').toLowerCase();

console.log(formatted);
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.4.0/luxon.min.js"></script>

You could also do this without a library, however you may find that not all browsers will parse the input string, and your output format is up to the browser as well.

// Get the current local date as a string
const date = new Date().toLocaleDateString();

// Parse the date and time in the local time zone
// Warning: This is non-standard and may fail in some environments
const dt = new Date(date + ' 1:30 pm');

// Format the output, converting to the desired time zone
const result = dt.toLocaleString(undefined, { timeZone: 'America/Los_Angeles' });

console.log(result);

There are, of course, manual ways to parse and format dates (using regex, etc.) but I'll leave that up to you or another person to complete.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575