5

I want to get Unix timestamp (time in seconds) from tomorrow.

I have tried the following with no success:

var d = new Date();
d.setDate(d.getDay() - 1);
d.setHours(0, 0, 0);
d.setMilliseconds(0);
console.log(d/1000|0)

How would I fix the above?

7
  • Tomorrow is today plus 1, not minus 1. And use .getDate() not .getDay(). Commented Apr 19, 2019 at 20:23
  • getDay() returns the time in ms of the current day so -1 or +1 does not do anything would getDate() change anything? Commented Apr 19, 2019 at 20:26
  • No, getDay() returns the day-of-the-week. Commented Apr 19, 2019 at 20:27
  • 2
    Perhaps you should spend some time with the documentation. Commented Apr 19, 2019 at 20:28
  • 1
    this should work as per problem description var d = new Date(); d.setDate(d.getDate() + 1); d.setHours(0, 0, 0); d.setMilliseconds(0); console.log(d) Commented Apr 19, 2019 at 20:36

4 Answers 4

3

Just modified your code and it works fine

var d = new Date();
d.setDate(d.getDate() + 1);
d.setHours(0, 0, 0);
d.setMilliseconds(0);
console.log(d)
>> Sun Apr 21 2019 00:00:00 GMT+0530 (India Standard Time)

Hope this will work for you

Sign up to request clarification or add additional context in comments.

Comments

1

This should do it.

Copied directly from https://javascript.info/task/get-seconds-to-tomorrow

function getSecondsToTomorrow() {
  let now = new Date();

  // tomorrow date
  let tomorrow = new Date(now.getFullYear(), now.getMonth(), now.getDate()+1);

  let diff = tomorrow - now; // difference in ms
  return Math.round(diff / 1000); // convert to seconds
}

console.log(getSecondsToTomorrow());

2 Comments

Or let toTomorrow = new Date().setHours(24,0,0,0) - Date.now(). ;-) If you think it's a duplicate, you should mark it as such, not just copy another answer into an answer here.
@RobG not a duplicate. That answer is from a completely different website.
-1

you could use a third party library like moment js which makes your life alot easier

momentjs

1 Comment

Saying "use library X" isn't an answer.
-2

You can use a unix timestamp and add 24*60*60*1000 (same as 86400000) to the current time's timestamp. You can then pass that to new Date() like this:

  • 24 = hours
  • 60 = minutes
  • 60 = seconds
  • 1000 = converts the result to milliseconds

// Current timestamp
const now = Date.now()

// Get 24 hours from now
const next = new Date(now + (24*60*60*1000))

// Create tomorrow's date
const t = new Date(next.getFullYear(), next.getMonth(), next.getDate())

// Subtract the two and divide by 1000
console.log(Math.round((t.getTime() - now) / 1000), 'seconds until tomorrow')

1 Comment

This would give incorrect results around handle leap seconds and daylight savings time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.