0

i have this variables:

const startTime = Date.now();
const endTime = startTime + 555555;    <-----  555555 its about 6 days in seconds
const remainingTime = endTime - startTime;
const days = Math.ceil(remainingTime / daySeconds);
const daysDuration = days * daySeconds;

I need instead of 555555 to have a value in seconds until the first day of the next month

Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
Roman Karpyshyn
  • 159
  • 3
  • 10
  • 1
    Does this answer your question? [JavaScript - Get minutes between two dates](https://stackoverflow.com/questions/7709803/javascript-get-minutes-between-two-dates) (Just don't divide seconds by 60) – Heretic Monkey Apr 28 '20 at 19:55
  • Does this answer your question? [JavaScript - Converting a Date() into seconds](https://stackoverflow.com/questions/32208476/javascript-converting-a-date-into-seconds) – A. Meshu Apr 28 '20 at 19:56
  • Does this answer your question? [Javascript Date: next month](https://stackoverflow.com/questions/499838/javascript-date-next-month) – Triby Apr 28 '20 at 19:57
  • BTW, [`Date.now()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now) returns a value in milliseconds, not seconds. – Heretic Monkey Apr 28 '20 at 19:57

2 Answers2

1

Simple as that:

const now = new Date(),
      date = new Date()
      
      date.setMonth(date.getMonth()+1)
      date.setDate(1)
      date.setHours(0,0,0,0)
      
const deltaInSeconds = 0|(date-now)/1000

console.log(deltaInSeconds)
.as-console-wrapper{min-height:100%;}
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
0
const endTime = startTime + (
  new Date(
     Date.now().getFullYear(),
     Date.now().getMonth(),
     Date.now().getDay()
  )
  - Date.now();
);

I think theres better ways to do this.

Stephen Duffy
  • 467
  • 2
  • 14