在Angular中,可以使用DatePipe
来格式化日期时间,并且可以通过设置timezone
参数来控制是否进行时区转换。如果要显示不进行时区转换的日期时间,可以将timezone
参数设置为'UTC'
。
以下是一个示例代码:
在组件中引入DatePipe
:
import { DatePipe } from '@angular/common';
在组件的构造函数中注入DatePipe
:
constructor(private datePipe: DatePipe) {}
在模板中使用DatePipe
来格式化日期时间:
{{ myDate | date:'yyyy-MM-dd HH:mm:ss':'UTC' }}
在上面的示例中,myDate
是一个日期对象,date
管道将日期对象格式化为指定的格式,并使用'UTC'
时区参数来显示日期时间。
请注意,如果要使用DatePipe
,您需要在模块文件中将CommonModule
导入到imports
数组中,并将DatePipe
提供者添加到providers
数组中:
import { CommonModule, DatePipe } from '@angular/common';
@NgModule({
imports: [CommonModule],
providers: [DatePipe],
// ...
})
export class YourModule { }
这样就可以在Angular应用中显示不进行时区转换的日期时间了。