[Angular] Custom a DatePipe to support special time zone conversion from user

这篇具有很好参考价值的文章主要介绍了[Angular] Custom a DatePipe to support special time zone conversion from user。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Background

It didn't use a time zone format of datetime in database, data has only one time zone.

 a. Columns:

[Angular] Custom a DatePipe to support special time zone conversion from user,UI,前端,angular

 b. Data:

 [Angular] Custom a DatePipe to support special time zone conversion from user,UI,前端,angular

 So I'll define a custom DatePipe to extend DatePipe in my source code of Angular.

 How to implement?

1. Create a pipe class of Angular called MyDatePipe.

Output format: { yyyy/MM/dd HH:mm:ss }
import { DatePipe } from '@angular/common';
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
  name: 'myDate',
  standalone: true
})
export class MyDatePipe extends DatePipe implements PipeTransform {
  override transform(value: any, args?: any): any {
    let myDate = new Date(value);
    myDate.setHours(myDate.getHours() + args);
    return super.transform(myDate, 'yyyy/MM/dd HH:mm:ss');
  }
}

2. Using it in your component

a. Component

  providers: [MyDatePipe],

  imports: [MyDatePipe]

import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { TokenKey, TokenPre } from '@app/config/constant';
import { WindowService } from '@app/core/services/common/window.service';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzInputModule } from 'ng-zorro-antd/input';

import { MyDatePipe } from '../../pipes/my-datepipe';

@Component({
  selector: 'app-pages-empty',
  templateUrl: './empty.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: true,
  providers: [MyDatePipe],
  imports: [NzButtonModule, NzInputModule, FormsModule, MyDatePipe]
})
export class EmptyComponent implements OnInit {
  token?: string;
  srvDate: Date = new Date();
  srvOffsetOfTimeZone: number = 8; // offset of server time zone
  userOffsetOfTimeZone: number = -5.5; // offset of user time zone

  constructor(private windowServe: WindowService, private myDatePipe: MyDatePipe) {}

  ngOnInit(): void {}

  setToken(): void {
    this.windowServe.setSessionStorage(TokenKey, TokenPre + this.token);
  }

  getDate(): void {
    this.srvDate = new Date();
    const userDate = this.myDatePipe.transform(this.srvDate, -this.srvOffsetOfTimeZone + this.userOffsetOfTimeZone);
    console.log(`User date: ${userDate}`);
  }
}

b. Html


<button nz-button (click)="getDate()">Show user date</button>
Server time(UTC {{ srvOffsetOfTimeZone }}):{{ srvDate }}
<br />

UTC time(UTC): {{ srvDate | myDate : -srvOffsetOfTimeZone }}
<br />
User time(UTC {{ userOffsetOfTimeZone }}):{{ srvDate | myDate : -srvOffsetOfTimeZone + userOffsetOfTimeZone }}

3. Test result

[Angular] Custom a DatePipe to support special time zone conversion from user,UI,前端,angular文章来源地址https://www.toymoban.com/news/detail-679914.html

到了这里,关于[Angular] Custom a DatePipe to support special time zone conversion from user的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 深入Angular:(转&翻)Do you still think that NgZone (zone.js) is required for change detection in Angular?

    原文链接:IndepthApp 前言概览:注意区分NgZone和zone.js, 更多细节在Angular跟新策略篇,尚未翻译完成。 本文主要解释了Angular是如何基于zone.js实现NgZone。 同时阐述如何在不使用zone.js的情况下,实现手动更新。文章最后部分将描述自动跟新策略何时会失效。 我看过的大多数文章

    2024年01月16日
    浏览(42)
  • com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value ‘Öйú±ê×解决方案

      大家好,我是爱编程的喵喵。双985硕士毕业,现担任全栈工程师一职,热衷于将数据思维应用到工作与生活中。从事机器学习以及相关的前后端开发工作。曾在阿里云、科大讯飞、CCF等比赛获得多次Top名次。现为CSDN博客专家、人工智能领域优质创作者。喜欢通过博客创作

    2024年02月03日
    浏览(26)
  • Infragistics Adds Support for Angular 17

    Ignite UI for Angular 23.1 (17.0.0) takes advantage of the performance related optimizations in the latest Angular release. Infragistics Ignite UI for Angular is a comprehensive library of Angular-native UI controls and Material-based UI components, which includes a high-performance data grid and over 60 charts. Its WYSIWYG app builder helps you streamline y

    2024年02月04日
    浏览(31)
  • 解决copilot报错:Your current Copilot license doesn‘t support proxy connections with custom certificates

    如题,github copilot在安装插件并且成功登录github的情况下尝试使用时报: 查阅了官方trouble shooting文档发现并没有针对这一报错的解决方法,网上的各种问题也没有一样的情况,并且解决措施都没有效果。 本人在尝试https://blog.csdn.net/tbicf/article/details/131548228这篇文章的解决方法

    2024年01月22日
    浏览(56)
  • [23] Instruct 3D-to-3D: Text Instruction Guided 3D-to-3D conversion

    本文提出一种3D-to-3D转换方法:Instruct 3D-to-3D; 借助预训练的Image-to-Image扩散模型,本文方法可以使各个视角图片的似然最大;本文方法显式地将source 3D场景作为condition,可以有效提升3D连续性和可控性。 同时,本文还提出dynamic scaling,使得几何变换的强度是可调整的。 目录

    2024年02月13日
    浏览(23)
  • Vue报错:may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore

    找到项目根目录下的bulid文件夹下的 webpack.base.conf.js,找到以下代码块并注释掉第三行代码 注释完保存退出,重新启动项目即可。 关闭eslint语法检测,在 .eslintrc.js文件中 ,注释掉 eslint:recommended。 当项目并不存在build文件夹,即不属于vue脚手架工程,那请到根目录下 config文

    2024年01月23日
    浏览(41)
  • You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the

    问题: Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file. 解决方法 找到build目录下的webpack.base.conf.js文件,注释掉其中的与有关的eslint规则即可。 关闭编码规范检查,编辑webpack.base.conf.js,找到module节点下的rules节点,注释掉这一行:

    2024年04月09日
    浏览(42)
  • Custom Filters to Dashboard Crack

    Stimulsoft Dashboards 2024.1.1 enhances data exploration capabilities for tables with the addition of user-defined filtering. Stimulsoft Dashboards is a versatile suite of tools for crafting interactive data visualizations for web and desktop applications. It empowers developers to embed intuitive dashboards across various platforms, including .NET, JavaScri

    2024年01月22日
    浏览(24)
  • vue解决:You may use special comments to disable some warnings.Use // eslint-disable-next-line to ign

    错误描述:项目启动时,出现    You may use special comments to disable some warnings.的翻译是: 你可以使用一些特殊的注释来禁用一些警告 出现这样的问题是:ESLint 对语法的要求过于严格,出现这样的问题并不是写的代码有异常,是代码的格式有问题 解决办法:取消ESLint验证规则

    2024年02月16日
    浏览(44)
  • Java 8 date/time type `java.time.LocalDateTime` not supported by default:日期序列化问题

    解决办法一:将实体类中的LocalDateTime转为Date类型 解决办法二: 通过注解指定 配置全局 解决办法三: 用阿里的FastJson替换ObjectMapper

    2024年02月12日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包