-1

I want to add one day or 12 hours to a date in the format of

Thu Mar 03 2022 12:00:00 GMT

I have tried:

new Date(value.startDate + 1);

but it does nothing.

Please help me out, I am new to JavaScript.

La Lisa
  • 19
  • 2
  • 1
    Welcome to Stack Overflow! Visit the [help], take the [tour] to see what and [ask]. Please first ***>>>[Search for related topics on SO](https://www.google.com/search?q=javascript+add+one+day+to+date+site%3Astackoverflow.com)<<<*** and if you get stuck, post a [mcve] of your attempt, noting input and expected output using the [`[<>]`](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Feb 14 '22 at 12:34
  • _one day or 12 hours_? Which do you want? – mplungjan Feb 14 '22 at 12:34
  • @mplungjan 12 hours – La Lisa Feb 14 '22 at 12:53
  • 1
    `let d=new Date(value.startDate); d.setHours(d.getHours()+12)` – mplungjan Feb 14 '22 at 12:57

2 Answers2

1

Try this

const date = new Date("Thu Mar 03 2022 12:00:00 GMT");

date.setDate(date.getDate() + 1);
1

If you want to add something to your timestamp, this will do the trick no matter what you want to add.

const timestamp = new Date("Thu Mar 03 2022 12:00:00 GMT")
console.log(timestamp.toString())

// because getTime and setTime uses milliseconds:
const millisecondsToBeAdded = 12 * 60 * 60 * 1000
timestamp.setTime(timestamp.getTime() + millisecondsToBeAdded)

console.log(timestamp.toString())
Sebastian
  • 1,321
  • 9
  • 21