How To Add or Subtract Hours, Minutes, or Seconds from a Date in JavaScript with date-fns

node v18.11.0
version: 1.0.0
endpointsharetweet
// import isn't supported in Node v18.11.0 otherwise it would be // import { addSeconds, subSeconds, addMinutes, subMinutes, addHours, subHours } from 'date-fns' const dateFns = require( "date-fns"); const { addSeconds, subSeconds, addMinutes, subMinutes, addHours, subHours } = dateFns; // 31th of January 2023 const date = new Date(2023, 0, 31, 10, 18, 35); console.log(date); console.log('Add 15 seconds: ', addSeconds(date, 15)); console.log('Subtract 2 seconds: ', subSeconds(date, 2)); console.log('Add 15 minutes: ', addMinutes(date, 15)); console.log('Subtract 2 minutes: ', subMinutes(date, 2)); console.log('Add 15 hours: ', addHours(date, 15)); console.log('Subtract 2 hours: ', subHours(date, 2));
Loading…

no comments

    sign in to comment