【发布时间】:2018-10-31 19:59:22
【问题描述】:
我有一个下拉菜单可以在 UI 中选择时区。 从 Windows 时区设置下拉列表中获取的下拉数据
如果登录用户选择了某个时区,我必须根据所选时区格式使用 DST 显示所有日期时间字段。
Typescript 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。需要帮助。
【问题讨论】:
-
如果您在服务器端代码中运行 .NET,则使用 TimeZoneConverter 可以更好地处理服务器端。
-
另外,这里显示的代码有几个错误。您不应该尝试手动为 DST 添加一小时。 Moment-timezone 内置了所有这些。此外,除了 tt 到 a 之外,您可能还有其他几种替代方法。
标签: c# timezone momentjs aurelia moment-timezone