2

i have a dropdown to select timezone in UI. enter image description here that dropdown data taken from windows timezone settings dropdown list enter image description here

If logged user in selected some timezone, i have to display all datetime fields according to selected timezone format with DST.

My table structure is enter image description here

Typescript ts code

import * as moment from 'moment';
import * as momenttimezone from 'moment-timezone';

private ConvertServerTimezoneToClient(dateTime: string, dateFormat: string, timeFormat: string, timezoneFormat: string, isDstzone: string) {
    timeFormat = timeFormat.toString().indexOf('tt') > -1 ? timeFormat.replace('tt', 'a') : timeFormat;
    var convertedTime = '';
    if (timezoneFormat && timezoneFormat != '' && timezoneFormat != "null") {
        if (isDstzone == 'true') {
            momenttimezone.tz.add(''); // need to map
            momenttimezone.tz.link(''); // need to map
            var zoneName = ''; // need to map
            var isDstDate = momenttimezone.tz(new Date(dateTime), zoneName).isDST();
            if (isDstDate) {
                convertedTime = moment(dateTime).zone(timezoneFormat).add(1, 'hours').format(dateFormat + ' ' + timeFormat);
            } else {
                convertedTime = moment(dateTime).zone(timezoneFormat).format(dateFormat + ' ' + timeFormat);
            }
        }
        else {
            convertedTime = moment(dateTime).zone(timezoneFormat).format(dateFormat + ' ' + timeFormat);
        }
    }
    return convertedTime
}

Moment js have more timezone formats https://github.com/moment/moment-timezone/blob/develop/data/packed/latest.json

How to map windows timezone to moment timezone. UI base code is with aurelia typescript. Need help.

gowtham ks
  • 427
  • 5
  • 6
  • This is better handled server side with [TimeZoneConverter](https://github.com/mj1856/TimeZoneConverter), assuming you are running .NET in your server-side code. – Matt Johnson-Pint May 22 '18 at 15:38
  • Also, your code shown here has several bugs. You should not be trying to add an hour for DST manually. Moment-timezone has all that built in. Also you probably have several other susbtitutions to make besides tt to a. – Matt Johnson-Pint May 22 '18 at 15:42

1 Answers1

2

There is no quick or easy way to do this. Momentjs doesn't support it and isn't planning to either - for the same reason why you should really be doing this on the server, rather than on the client. If you need your timezones in Windows format in .NET code, use NodaTime on your server to do this.

If for one reason or another you still insist on doing this on the client, you could use the official file for conversion between Windows and IANA timezones. Parse that file and then use it to do your mappings.

All else aside, I would strongly recommend working with UNIX timestamps instead of strings. It's much easier to pass an unambigious number around that every decent DateTime library will know how to convert properly, than to keep track of and properly parse a string timestamp everywhere.

Fred Kleuver
  • 7,797
  • 2
  • 27
  • 38