To transform UTC time to local time in Angular, you have a couple of options at your disposal. The first approach involves utilizing the built-in DatePipe for conversion, while the second method makes use of the Date constructor and the toLocaleString() function. Here are two different ways you can achieve this:
import { DatePipe } from '@angular/common';
// Implementation within your component or service
export class YourComponentOrService {
constructor(private datePipe: DatePipe) {}
convertUTCtoLocal(utcDate: string): string {
const localDate = this.datePipe.transform(utcDate, 'medium', '+0530');
return localDate;
}
}
Alternatively, you can utilize the Date constructor like so:
export class YourComponentOrService {
convertUTCtoLocal(utcDate: string): string {
const date = new Date(utcDate);
const localDate = date.toLocaleString('en-IN', {timeZone: 'Asia/Kolkata'});
return localDate;
}
}