Does anybody know of an easy way of taking a date (e.g. Today) and going back X days?
So, for example, if I want to calculate the date 5 days before today.
Does anybody know of an easy way of taking a date (e.g. Today) and going back X days?
So, for example, if I want to calculate the date 5 days before today.
If you want to both subtract a number of days and format your date in a human readable format, you should consider creating a custom
DateHelper
object that looks something like this :(see also this Fiddle)
I like the following because it is one line. Not perfect with DST changes but usually good enough for my needs.
I converted into millisecond and deducted days else month and year won't change and logical
Hope helps
A few of the existing solutions were close, but not quite exactly what I wanted. This function works with both positive or negative values and handles boundary cases.
Try something like this:
Note that this modifies the date object and returns the time value of the updated date.
When setting the date, the date converts to milliseconds, so you need to convert it back to a date:
This method also take into consideration, new year change etc.