0

Ex - Suppose I select a date and time in timezone (IST) +05:30 i. e. date- 10/15/2020, time- 10:29

then How Do I convert date and time in Different time Zone Like like CDT, ACT, IT, ITA..

Example - It should be change date and time in timezone (CDT) -06:00 like date- 10/14/2020, time- 22:59

Divya
  • 7
  • 3
  • For this and most other time- and date-related manipulations in Javascript, I would encourage you to look into https://momentjs.com/. – Myk Willis Oct 15 '20 at 05:06
  • 2
    Does this answer your question? [Convert date to another timezone in JavaScript](https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – deepak thomas Oct 15 '20 at 05:08

3 Answers3

2

You can utilize Angular's Date Pipe for it

Have attached a Stackblitz Demo for your reference

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");
  }

}

You can also do it like this:

medium is a format and CST is the timezone or you can supply any values you want.

<pre>{{ now | date: 'medium' : 'CST' }}</pre>
KShewengger
  • 7,853
  • 3
  • 24
  • 36
1

use momentjs to change the timezone

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

need to change the region of your specific timezone here you want to convert IST to CST

please go through https://momentjs.com/timezone/docs/#/using-timezones/converting-to-zone/

Mohammad Zubair
  • 413
  • 4
  • 12
  • var cst_time = moment.tz("2020-01-29 10:52:00", "America/Chicago"); how do I get 2020-01-29 10:52:00 if I have only TimeZone Like -6:00 and +5:30 – Divya Oct 16 '20 at 07:43
-1

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())

OR

For moment.js users, you can now use https://momentjs.com/timezone/

Prathamesh More
  • 1,470
  • 2
  • 18
  • 32
G Babbar
  • 1
  • 1