To use the sub
function from the date-fns
package in Javascript, first you need to install the package:
index.tsx21 chars2 lines
Then, you can import the function and use it to subtract a specified amount of time from a given date.
Here is an example of using the sub
function to subtract 1 day from the current date:
index.tsx171 chars7 lines
In this example, the sub
function accepts two arguments - the date to be subtracted from and an object specifying the amount of time to subtract. The properties of this object should match the supported time units, such as days
, hours
, minutes
, seconds
, and so on.
In the code above, we are subtracting 1
day from the current date.
You can also subtract different combinations of time units by passing an object with multiple properties to sub
:
index.tsx144 chars3 lines
Note that the subtracted time is relative to the current date and time by default, but you can pass a second argument specifying the reference date - see the date-fns
documentation for more information.
gistlibby LogSnag