3 回答

TA貢獻1871條經驗 獲得超8個贊
您可以為此使用Angular 的日期管道
import { DatePipe } from "@angular/common";
@Component({
? selector: "my-date",
? templateUrl: "./date.component.html",
? providers: [DatePipe]? ? ? ? ? ? ?// Add DatePipe as Provider
})
export class DateComponent {
? now: any = Date.now();
? format: string = "medium";? ? ? ?// more formats are available inside the DatePipe Documentation
? constructor(private datePipe: DatePipe) {}? ? // Initialize DatePipe inside the constructor
? get dateInGMT(): any {
? ? // 1st parameter is the date
? ? // 2nd parameter is the format
? ? // 3rd parameter is the time zone
? ? return this.datePipe.transform(this.now, this.format, "GMT");
? }
? get dateInCEST(): any {
? ? return this.datePipe.transform(this.now, this.format, "CEST");
? }
? get dateInCustom(): any {
? ? return this.datePipe.transform(this.now, this.format, "+0530");
? }
}
你也可以這樣做:
medium是一種格式,CST是時區,或者您可以提供您想要的任何值。
<pre>{{ now | date: 'medium' : 'CST' }}</pre>

TA貢獻1795條經驗 獲得超7個贊
使用 momentjs 更改時區
var?cst_time?=?moment.tz("2020-01-29?10:52:00",?"America/Chicago");??//?-6:00 var?ist_time?=?cst_time?.tz('Asia/Calcutta').format('MMMM?Do?YYYY,?h:mm:ss?a');?//?+5:30
需要在此處更改您要將 IST 轉換為 CST 的特定時區的區域

TA貢獻1789條經驗 獲得超10個贊
var aestTime = new Date().toLocaleString("en-US", {
? timeZone: "Australia/Brisbane"
});
console.log('AEST time: ' + (new Date(aestTime)).toISOString())
var asiaTime = new Date().toLocaleString("en-US", {
? timeZone: "Asia/Shanghai"
});
console.log('Asia time: ' + (new Date(asiaTime)).toISOString())
var usaTime = new Date().toLocaleString("en-US", {
? timeZone: "America/New_York"
});
console.log('USA time: ' + (new Date(usaTime)).toISOString())
var indiaTime = new Date().toLocaleString("en-US", {
? timeZone: "Asia/Kolkata"
});
console.log('India time: ' + (new Date(indiaTime)).toISOString())
添加回答
舉報