[英]I have a time and a time zone - no date. How do I convert that time to another time zone?
[英]How do I convert date and time in different Time Zone?
Ex - 假設我在時區(IST) +05:30 中選擇一個日期和時間,即 date- 10/15/2020 ,time- 10:29
那么我如何轉換不同時區的日期和時間,比如 CDT、ACT、IT、ITA ..
示例 - 它應該在時區(CDT) -06:00 中更改日期和時間,例如 date- 10/14/2020 ,time- 22:59
您可以使用Angular 的日期管道
附上Stackblitz Demo供您參考
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>
使用 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 的特定時區的區域
請通過https://momentjs.com/timezone/docs/#/using-timezones/converting-to-zone/
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())
或者
對於 moment.js 用戶,您現在可以使用https://momentjs.com/timezone/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.