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: 
As others have said you're subtracting from the numeric values returned from methods like `date.getDate()`, you need to reset those values on your date variable. I've created a method below that will do this for you. It creates a date using `new Date()` which will initialize with the current date, then sets the date, month, and year according to the values passed in. For example, if you want to go back 6 days then pass in -6 like so `var newdate = createDate(-6,0,0)`. If you don't want to set a value pass in a zero (or you could set default values). The method will return the new date for you (tested in Chrome and Firefox).

```
function createDate(days, months, years) {
        var date = new Date(); 
        date.setDate(date.getDate() + days);
        date.setMonth(date.getMonth() + months);
        date.setFullYear(date.getFullYear() + years);
        return date;    
    }

```
Vanilla JS `Date` saves the time as milliseconds from [Epoch time](https://en.wikipedia.org/wiki/Epoch_(computing)) (1970), so all we need to do is subtract in milliseconds.
if we want to subtract 10 days we would subtract 1000(ms) \* 60(s) \* 60(m) \* 24(h) \* 10(d)

A simple Vanilla function to subtract from a date (notice the `years` exception):

```js
subtractFromDate(new Date(), { hours: 15 }) // now - 15 hours

```

```js
subtractFromDate(new Date(), { days: 15 }) // now - 15 days

```

```js
subtractFromDate(new Date(), { years: 15 }) // now - 15 years

```

```js
const subtractFromDate = (
    date,
    { years, days, hours, minutes, seconds, milliseconds } = {}
) => {
    const millisecondsOffset = milliseconds ?? 0
    const secondsOffset = seconds ? 1000 * seconds : 0
    const minutesOffset = minutes ? 1000 * 60 * minutes : 0
    const hoursOffset = hours ? 1000 * 60 * 60 * hours : 0
    const daysOffset = days ? 1000 * 60 * 60 * 24 * days : 0
    const dateOffset =
        millisecondsOffset +
        secondsOffset +
        minutesOffset +
        hoursOffset +
        daysOffset

    let newDate = date
    if (years) newDate = date.setFullYear(date.getFullYear() - years)
    newDate = new Date(newDate - dateOffset)

    return newDate
}

```