Convert a date from Central Standard Time (CST) to Indian Standard Time (IST)

By | January 2, 2024
import Foundation

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(identifier: "America/Chicago") // CST
let dateInCST = dateFormatter.date(from: "2023-01-01 12:00:00")!

dateFormatter.timeZone = TimeZone(identifier: "Asia/Kolkata") // IST
let dateInIST = dateFormatter.string(from: dateInCST)

print(dateInIST)

Java:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class CSTtoIST {
    public static void main(String[] args) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("America/Chicago")); // CST
        Date dateInCST = dateFormat.parse("2023-01-01 12:00:00");

        dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); // IST
        String dateInIST = dateFormat.format(dateInCST);

        System.out.println(dateInIST);
    }
}

Python:

from datetime import datetime
from pytz import timezone

cst = timezone('America/Chicago')  # CST
ist = timezone('Asia/Kolkata')  # IST

date_str = "2023-01-01 12:00:00"
date_format = "%Y-%m-%d %H:%M:%S"
date_in_cst = cst.localize(datetime.strptime(date_str, date_format))
date_in_ist = date_in_cst.astimezone(ist)

print(date_in_ist.strftime(date_format))

JavaScript:

const dateStr = "2023-01-01 12:00:00";
const cstTime = new Date(dateStr + " GMT-0600"); // CST
const istTime = cstTime.toLocaleString("en-US", { timeZone: "Asia/Kolkata" }); // IST

console.log(istTime);

These examples assume a specific date and time format. You should adjust the date format and time zone identifiers according to your specific requirements.

Leave a Reply

Your email address will not be published. Required fields are marked *