

This is like chaos's answer, but in one line: var newDateObj = new Date(oldDateObj.getTime() + diff*60000) For example, with Moment.js, this is simply: var newDateObj = moment(oldDateObj).add(30, 'm').toDate() If you are doing a lot of date work, you may want to look into JavaScript date libraries like Luxon, Day.js, or Moment.js. Then, as I drag each range input around, we can see how it affects the. This.formattedDate = ( result, this.dateMask ) Īs you can see, in my ngDoCheck() life-cycle method, I'm just taking all of the current deltas and I'm applying them to the base-date, which is whatever time it was when the demo is loaded. Result = ( "millisecond", this.millisecondDelta, result ) Result = ( "second", condDelta, result ) Result = ( "minute", this.minuteDelta, result ) Result = ( "hour", this.hourDelta, result ) Result = ( "day", this.dayDelta, result ) Result = ( "month", this.monthDelta, result ) Since there are 3600000 milliseconds in 1 hour, we can add multiples of 3600000 to our converted date to add as many hours as we want. The getTime() method converts dates into the number of milliseconds since January 1st, 1970. Result = ( "year", this.yearDelta, result ) To add hours to a date, we can use the JavaScript getTime()method. add() function returns a NEW date each time, so we have to keep saving that a new digest will be triggered on every (input) event. date-delta is adjusted, we're just going to hook into the digest since we know
NOTE: Rather than have an explicit function that has to get called every time a Setting up DateTimePicker: Step 1: Include Bootstrap and jQuery CDN into your before all other stylesheets to load our CSS.This.dateMask = "yyyy-MM-dd HH:mm:ss.SSS" But, unlike the native JavaScript behavior, I'm going to make a copy of the original Date so that we don't mutate the input: The "part" denotes which "field" within the input Date is going to be used, and then applies the relevant. add( part: string, delta: number: input: Date ) : Date


To wrap this date add/subtract up, I've crated a DateHelper class that exposes an. Well, we can do the same thing in Angular. Of course, as I said above, other languages make this behavior a bit more palatable with some sort of dateAdd() function.
#JAVASCRIPT SETDATE ADD HOURS CODE#
Improve this sample solution and post your code through Disqus.

So, I can easily subtract 48-hours from a given date with the same function: See the Pen JavaScript - Add specified years to a date-date-ex-41 by w3resource (w3resource) on CodePen. These types of functions work just as well negative deltas. For example, in the Lucee CFML / ColdFusion world, I can easily add 48-hours to a given date with the dateAdd() function: I'm sure that all server-side languages have ways to add and remove time from a given date. View this code in my JavaScript Demos project on GitHub. Run this demo in my JavaScript Demos project on GitHub.
