
Which is far easier to read and less mistake-prone than doing it in vanilla JavaScript. The documentation is great, and it simplifies date handling while making it more reliable.įor example, to add 3 days and 2 minutes to the current date you’d use: moment().add(7, 'days').add(2, 'minutes') Moment.js provides the tools to manage dates, timezones, time periods (the period between two dates) – all conveniently wrapped in easily used classes.

Return Value: This method returns the result object subtracting date2 from date1. If you build applications that deal with dates frequently, Moment.js is invaluable. date1: It is the first date object from which the second date is subtracted. You can supply negative numbers or use the – (subtraction) operator instead of addition. The resulting date will be today’s date plus 4 days, 3 hours, 2 minutes, and 1 second.

The getTime() method returns the number of milliseconds since January 1, 1970, so we can use it to alter the value of the date by any number of millisecondsĭate.setTime(date.getTime() + (addHours * 60 * 60 * 1000)) // Convert hours to milliseconds - 60 minutes to an hour, 60 seconds to a minute, 1000 milliseconds to a secondĭate.setTime(date.getTime() + (addDays * 24 * 60 * 60 * 1000)) // Similar to above, but additionally multiplying by 24 as there are 24 hours in a dayĭate.setTime(date.getTime() + (addMinutes * 60 * 1000)) // Convert minutes to millisecondsĭate.setTime(date.getTime() + (addSeconds * 1000)) // Convert seconds to milliseconds Add Days to Date Using Vanilla JavaScriptįirst, here’s how it’s done in plain old JavaScript: var date = new Date() // Todays date - the Date() constructor will default to the current date/time if no value is passed to it Why would you want to do this? Perhaps you want to tell a user that their assignment is due in a week and provide the date, or maybe your application needs to know what the date was 30 days ago to pop up a reminder – there are near-infinite usage cases.
Js date minus minutes how to#
This article will explain how to add ( or subtract) days, minutes, hours, seconds (etc.) to a JavaScript date.
