gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
get current time IST in javascript
- User
get current time IST in javascript
- Assistant
To get the current time in Indian Standard Time (IST) using JavaScript, you can use the
toLocaleString()method along with the appropriate options. IST is UTC+5:30.Here's a simple way to do this:
// Get the current date and time const now = new Date(); // Get the IST time const options = { timeZone: 'Asia/Kolkata', // Use 'Asia/Kolkata' for IST year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false // Set to true if you want a 12-hour format }; // Convert to IST const istTimeString = now.toLocaleString('en-IN', options); console.log(istTimeString);Explanation:
- We create a new
Dateobject to get the current time. - We define options for formatting the date and time, specifying the
timeZoneas 'Asia/Kolkata' to get IST. toLocaleStringis used to convert the date object to a string based on the provided format and time zone.- The resulting string will represent the current time in IST.
- We create a new