2

我有一个下拉菜单可以在 UI 中选择时区。 在此处输入图像描述 从 Windows 时区设置下拉列表中获取的下拉数据 在此处输入图像描述

如果登录用户选择了某个时区,我必须根据所选时区格式使用 DST 显示所有日期时间字段。

我的表结构是 在此处输入图像描述

打字稿ts代码

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 有更多时区格式 https://github.com/moment/moment-timezone/blob/develop/data/packed/latest.json

如何将 windows 时区映射到时刻时区。UI 基本代码使用 aurelia typescript。需要帮忙。

4

1 回答 1

2

没有快速或简单的方法可以做到这一点。Momentjs 不支持它,也不打算支持它 - 出于同样的原因,您应该在服务器上而不是在客户端上执行此操作。如果您需要 .NET 代码中的 Windows 格式的时区,请在您的服务器上使用 NodaTime 来执行此操作

如果出于某种原因您仍然坚持在客户端执行此操作,您可以使用官方文件在 Windows 和 IANA 时区之间进行转换。解析该文件,然后使用它来进行映射。

抛开其他一切不谈,我强烈建议使用 UNIX 时间戳而不是字符串。传递一个明确的数字,让每个像样的 DateTime 库都知道如何正确转换,比在任何地方跟踪和正确解析字符串时间戳要容易得多。

于 2018-05-23T00:44:57.743 回答