Finding date by subtracting X number of days from a particular date in Javascript

后端 未结 9 1979
滥情空心
滥情空心 2020-11-27 05:21

I want to find date by subtracting X number of days from a particular date in JavaScript. My JavaScript function accepts 2 parameters. One is the date value and the other i

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 06:09

    Here's an example, however this does no kind of checking (for example if you use it on 2009/7/1 it'll use a negative day or throw an error.

    function subDate(o, days) {
    // keep in mind, months in javascript are 0-11
    return new Date(o.getFullYear(), o.getMonth(), o.getDate() - days);;
    }
    

提交回复
热议问题