Question: Suppose I have a function that returns the starting index (4) of the word 'world' in this HTML string:

```
<div>The world is round</div>

```

This function (outside of my control) works by using jQuery's `.text()` method. However, I need to use jQuery's `.html()` method for what I'm doing.

Using the existing index will give me the letter "n" from the span when using jQuery's `.html()` method. I need to alter this index to take account of additional HTML tags and nested HTML tags that might be in the div e.g.

```
<div><p><span class="highlight">The</span></p> world is round</div>

```

So, this code snippet would return a new index of 42 for the first letter of 'world'. How would I go about doing this? Many thanks!

UPDATE: The code snippet would return the index INCLUDING the tags. I'm not looking to strip the tags. Thanks!

Answer: 
This is a pure-function which takes a passed-in starting date, building on Phil's answer:

```
function deltaDate(input, days, months, years) {
    return new Date(
      input.getFullYear() + years, 
      input.getMonth() + months, 
      Math.min(
        input.getDate() + days,
        new Date(input.getFullYear() + years, input.getMonth() + months + 1, 0).getDate()
      )
    );
}

```

e.g. writes the date one month ago to the console log:

```
console.log(deltaDate(new Date(), 0, -1, 0));

```

e.g. subtracts a month from March 30, 2020:

```
console.log(deltaDate(new Date(2020, 2, 30), 0, -1, 0)); // Feb 29, 2020

```

Note that this works even if you go past the end of the month or year.

**Update:** As the example above shows, this has been updated to handle variances in the number of days in a month.
Use the [moment.js](https://momentjscom.readthedocs.io/en/latest/) library for time and date management. 

```
import moment = require('moment');

const now = moment();

now.subtract(7, 'seconds'); // 7 seconds ago
now.subtract(7, 'days');    // 7 days and 7 seconds ago
now.subtract(7, 'months');  // 7 months, 7 days and 7 seconds ago
now.subtract(7, 'years');   // 7 years, 7 months, 7 days and 7 seconds ago
// because `now` has been mutated, it no longer represents the current time

```