ABP 中的语言环境日期时间 [英] Locale DateTime in ABP

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

问题描述

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

HTML:

<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>

TS:

formatDate(date: any, format: string): string {如果(!日期){返回 '​​';}返回时刻(日期).格式(格式);}onShown(): 无效 {$(this.requiredByDate.nativeElement).datetimepicker({语言环境:abp.localization.currentLanguage.name,格式:'LT',});}

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

非常感谢任何输入:)

解决方案

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

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

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

场景如下:

  1. 假设这是保存在 DB 中的 UTC 时间:2018-02-23 00:00:00
  2. 在填充该字段时,它会向其添加 10:30(我的本地时区)并将其显示在该字段中:2018-02-23 10:30:00
  3. 我保存表单而不更改上述值
  4. 时刻再次从服务器返回值(2018-02-23 00:00:00)中扣除10:30小时,发送到服务器保存.
  5. 然后我为该字段设置了一个新值,而无需在表单中更改它 (2018-02-22 13:30:00)!

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',
    });
}

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!

Any input is much appreciated :)

解决方案

I ended up with changing my formatDate method to following:

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

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!

Here is the scenario:

  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 中的语言环境日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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