Roman Karpyshyn
Roman Karpyshyn

Reputation: 199

Calculate seconds until next month

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

Upvotes: 0

Views: 277

Answers (2)

Yevhen Horbunkov
Yevhen Horbunkov

Reputation: 15530

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%;}

Upvotes: 1

Stephen Duffy
Stephen Duffy

Reputation: 505

const endTime = startTime + (
  new Date(
     Date.now().getFullYear(),
     Date.now().getMonth(),
     Date.now().getDay()
  )
  - Date.now();
);

I think theres better ways to do this.

Upvotes: 0

Related Questions