Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

But I don't want a specific timezone. I want it to ignore the timezone. I knew it was being thrown off by timezone but was unsure why.

This is one of dozens sometimes hundreds of questions I need answers to every day and want to minimize the friction. I don't want to have to read this unless I don't have to. Sure sometimes if its important enough or I'm curious, but the ChatGPT answer was much better for me. And I get by fine on Google otherwise

function convertTZ(date, tzString) {

    return new Date((typeof date === "string" ? new Date(date) : date).toLocaleString("en-US", {timeZone: tzString}));   
}

// usage: Asia/Jakarta is GMT+7

convertTZ("2012/04/20 10:10:30 +0000", "Asia/Jakarta") // Tue Apr 20 2012 17:10:30 GMT+0700 (Western Indonesia Time)

// Resulting value is regular Date() object

const convertedDate = convertTZ("2012/04/20 10:10:30 +0000", "Asia/Jakarta")

convertedDate.getHours(); // 17

// Bonus: You can also put Date object to first arg

const date = new Date()

convertTZ(date, "Asia/Jakarta") // current date-time in jakarta.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: