![]() ![]() Output: New Date: Fri 13:57:40 GMT+0530 (India Standard Time)Īdd or Subtract Seconds: const date = new Date() ĭate.setSeconds(date. Following is the code to add one week with current date: Add one week with current date let numWeeks 1 let currentDateObj new Date() tDate(currentDateObj. We can get the current date as a number and then add or subtract 7 days with it. Output: New Date: Fri 23:56:37 GMT+0530 (India Standard Time)Īdd or Subtract Minutes: const date = new Date() ĭate.getMinutes(date.getMinutes() + additionOfMinutes) // For subtract use minus (-) Adding or subtracting one week from the current date is quite simple in JavaScript. Output: New Date: Thu 13:55:16 GMT+0530 (India Standard Time)Īdd or Subtract Hours: const date = new Date() ĭate.setHours(date.getHours() + additionOfHours) // For subtract use minus (-) Output: New Date: Tue 13:52:52 GMT+0530 (India Standard Time)Īdd or Subtract Years: const date = new Date() ĭate.setFullYear(date.getFullYear() + additionOfYears) // For subtract use minus (-) There are 4 ways to create a new date object: new Date () new Date (year, month, day, hours, minutes, seconds, milliseconds) new Date (milliseconds) new Date (date string) new Date () new Date () creates a new date object with the current date and time: Example const d new Date () Try it Yourself » Date objects are static. We have explained different addition and Subtraction with the example below: Add or Subtract Days: const date new Date() const additionOfDays 10 tDate(date.getDate() + additionOfDays) // For subtract use minus (-) console.log('New. In Javascript add or subtract different date parameters is as easy as other languages. Output: New Date: Mon 13:50:56 GMT+0530 (India Standard Time)Īdd or Subtract Months: const date = new Date() ĭate.setMonth(date.getMonth() + additionOfMonths) // For subtract use minus (-) Date and Time modification is a very common part of development. We have explained different addition and Subtraction with the example below: Add or Subtract Days: const date = new Date() ĭate.setDate(date.getDate() + additionOfDays) // For subtract use minus (-) Output: New Date: Fri 13:57:40 GMT+0530 (India Standard Time)Īdd or Subtract Seconds: const date = new Date() ĭate.setSeconds(date.Add and Subtract day, month, year in Javascript Dateĭate and Time modification is a very common part of development. Output: New Date: Fri 23:56:37 GMT+0530 (India Standard Time)Īdd or Subtract Minutes: const date = new Date() ĭate.getMinutes(date.getMinutes() + additionOfMinutes) // For subtract use minus (-) The JavaScript Date object automatically takes care of adjusting the year if subtracting X months from the date pushes us into the previous year. var tMinutes (date.getMinutes ()-30) console.log (minute) //it will print the time and date according to the above condition in Unix-timestamp format. Output: New Date: Thu 13:55:16 GMT+0530 (India Standard Time)Īdd or Subtract Hours: const date = new Date() ĭate.setHours(date.getHours() + additionOfHours) // For subtract use minus (-) The setMonth() method takes a zero-based value representing the month of the year (0 January, 1 February, etc.) and sets the value for the date. var datenew Date () //here I am using '-30' to subtract 30 minutes from the current time. Output: New Date: Tue 13:52:52 GMT+0530 (India Standard Time)Īdd or Subtract Years: const date = new Date() ĭate.setFullYear(date.getFullYear() + additionOfYears) // For subtract use minus (-) The setDate function is called on a Date object. Output: New Date: Mon 13:50:56 GMT+0530 (India Standard Time)Īdd or Subtract Months: const date = new Date() ĭate.setMonth(date.getMonth() + additionOfMonths) // For subtract use minus (-) setDate() returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC until the updated time. This will give you a date 12 months in the past, stored as the current date: tMonth (date. In Javascript add or subtract different date parameters is as easy as other languages. In the 'month' date type, within the 'date' object, subtract 12 from the current number. Add and Subtract day, month, year in Javascript Dateĭate and Time modification is a very common part of development.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |