![]() Finally, I am setting the updated time using the setTime() method.Īs you can see, the process is the same. I am getting the current time with the getTime() method after adding 1 minute to it. const date = new Date() ĭate.setTime(date.getTime() + 1 * 60 * 1000) There is no other difference between adding hours and minutes. Using the setTime() method: You have to add milliseconds equivalent to the number of minutes you want to add to the current time. Like the hours, you can also add any amount of minutes to a javascript Date object in 2 methods: with the setTime() or setMinutes() method. How to Add Minutes To a JavaScript Date Object Now, if you check your object, you will find that 2 hours have been added to the object. Finally, pass the updated number to the setHours() method as the first argument. This method will return the number of hours currently present in that object.Īdd how many hours you want to add to it. const date = new Date() Īfter creating a Date object, you can call the getHours() method on it. There is no need to convert it to milliseconds. ![]() This method directly accepts the number of hours that you want to add. Using the setHours() method: If you don't want to go through the hassle of converting hours to milliseconds, you could use the setHours() method. Finally, I am setting the updated time to the Date object using the setTime() method. So, I am getting the current time and then adding 1 hour by converting it into milliseconds to the current time. This method will return the time in milliseconds.Īlso Read: Factory Function in JavaScript Explained with Examples I can get the current time using the getTime() method. In this example, I am adding 1 hour to the existing time.īut to add 1 hour to the current time, I have to get the current time first from my Date object. Now I can call the setTime() method this object returned from the constructor function. I am creating a Date object using the Date() constructor function in JavaScript. const date = new Date() ĭate.setTime(date.getTime() + 1 * 60 * 60 * 1000) You have to convert your time into milliseconds before passing it to this method. It doesn't matter if you want to add hours or minutes. You have to pass this time in milliseconds. Using the setTime() method: This method accepts one parameter: time. You can use setTime() or setHours() method. To add hours to a Date in JavaScript, there are 2 methods. How to Add Hours To a JavaScript Date Object This section will see how to use pure JavaScript to add hours, minutes, and seconds to the Date object without any external libraries. Use Vanilla JavaScript To Add Time To a Date It will help you to write clean and reusable code. But if your application is complex and shows dates and times in multiple places with different formats, you should choose an external library. If your application needs a simple date manipulation to display it in a few places, plain JavaScript will be a good choice. That's why external liberty is very convenient to use in a complex application. With this one method, you can add any specific amount of time to the Date object. In the case of external libraries, Day.js and Moment.js provide an add() method. But if you don't want to use 3 separate methods, you have setTime() to change hours, minutes, and seconds with this one method. In JavaScript, you have to use setHours(), setMinutes() and setSeconds() methods to modify the Date object.
0 Comments
Leave a Reply. |