ABP中的语言环境DateTime [英] Locale DateTime in ABP

查看:112
本文介绍了ABP中的语言环境DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弹出模式下有一个DateTime字段,如下所示,该字段仅显示时间部分:

I have a DateTime field in popup modal as below that is supposed to only show the time part:

HTML:

<div class='input-group date'>
   <input class="form-control" type="datetime" #RequiredByDate name="RequiredByDate" [value]="formatDate(hitchRequest.requiredByDate, 'LT')" required>
   <span class="input-group-addon">
      <span class="fa fa-clock-o"></span>
   </span>
</div>

TS:

formatDate(date: any, format: string): string {
    if (!date) {
        return '';
    }

    return moment(date).format(format);
}

onShown(): void {
    $(this.requiredByDate.nativeElement).datetimepicker({
        locale: abp.localization.currentLanguage.name,
        format: 'LT',
    });
}

当我设置DateTime并单击保存"按钮时,momentjs会将其真正转换为UTC时间并将其发送到服务器,最终将其以UTC时间保存在DB中. 我的问题是关于何时将数据从服务器读回现场.我假设moment.js会将其重新转换回本地时区,就像设置其值时所做的一样(似乎并非如此)!

When I set the DateTime and hit the save button the momentjs truly converts it to UTC time and send it to the server and eventually it is saved in DB in UTC time. My question is about when reading the data back from the server to the field. I assumed that the moment.js would reconvert it back to the local timezone like what it does when setting its value which seems it is not the case!

非常感谢任何输入:)

推荐答案

我最终将formatDate方法更改为以下内容:

I ended up with changing my formatDate method to following:

formatDate(date: any, format: string): string {
    return moment.utc(date.toString()).local().format(format);
}

它显示了从数据库获取值时的本地时间,但是现在的问题是更新其值时.当我保存表单时,它将日期考虑为本地时间,并且每次从日期中扣除10:30,然后将其发送到服务器!

It show the local time when getting the value from DB, but now the issue is when updating its value. When I save the form it considers the date to a local time and everytime it deducts 10:30 from it and then send to the server!

这是场景:

  1. 假设此UTC时间保存在数据库中:2018-02-23 00:00:00
  2. 在填充字段时,它将添加10:30(我的本地时区)并显示在字段中:2018-02-23 10:30:00
  3. 我保存表格而不更改上面的值
  4. 此刻再次从服务器返回的值中扣除10:30小时(2018-02-23 00:00:00),并将其发送到服务器以进行保存.
  5. 然后我对该字段有了一个新值,而没有更改其格式(2018-02- 22 13:30:00)!
  1. Assume this the UTC time saved in DB: 2018-02-23 00:00:00
  2. On populating the field it adds 10:30 (my local time zone) to it and shows it in the field: 2018-02-23 10:30:00
  3. I save the form without changing the above value
  4. The moment deducts 10:30 hours from the returned value from the server (2018-02-23 00:00:00) again and sends it to the server to be saved.
  5. Then I have a new value for the field without changing it in the form (2018-02-22 13:30:00)!

这篇关于ABP中的语言环境DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆