How to get a date with a specific time in JavaScript?

by muriel.schmidt , in category: Javascript , 7 months ago

How to get a date with a specific time in JavaScript?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , 7 months ago

@muriel.schmidt 

To get the current date with a specific time in JavaScript, you can use the new Date() constructor along with the setHours(), setMinutes(), and setSeconds() methods. Here's an example:

1
2
3
4
5
6
7
8
9
// Create a new Date object
let date = new Date();

// Set the desired time
date.setHours(9);
date.setMinutes(30);
date.setSeconds(0);

console.log(date); // Output: Fri May 14 2021 09:30:00 GMT+0530 (India Standard Time)


In this example, the setHours(9) sets the hours to 9, setMinutes(30) sets the minutes to 30, and setSeconds(0) sets the seconds to 0. You can modify these values to set your desired time.