get IST time in the format of 1-3-2024 7:26 (day month year time (12 hour format) in…
- User
get IST time in the format of 1-3-2024 7:26 (day month year time (12 hour format) in node.js
- Assistant
You can get the current time in Indian Standard Time (IST) in the specified format (day-month-year hours:minutes in 12-hour format) using Node.js. Here's a sample code that demonstrates how to do this using the
date-fnslibrary for formatting the date anddate-fns-tzfor handling time zones.First, you'll need to install the necessary libraries if you haven't already:
npm install date-fns date-fns-tzNow, you can use the following code to get the current time in IST:
const { format, toZonedTime } = require('date-fns-tz'); const getISTTime = () => { // Get the current date const currentDate = new Date(); // Convert to IST timezone const istDate = toZonedTime(currentDate, 'Asia/Kolkata'); // Format the date to "d-M-yyyy h:mm a" const formattedDate = format(istDate, 'd-M-yyyy h:mm a'); return formattedDate; }; console.log(getISTTime());