0

I'm making a Discord bot that I want to have a reward that you can claim once per day, and if you've already claimed it, it tells you how much time left. I've made the function that converts milliseconds into a readable format, but somehow using Date objects isn't working. Here is my code to get the timestamp in ms of the next claim time:

const nextdate = new Date(oldms + 86400000); // oldms is the miliseconds timestamp of the last claim time, this adds 24 hours to it.
// The next two lines are to set it to 00:00:00 so it can show the time until properly.
const regex = new RegExp(" ..:..:.. GMT");
const realnextdate = new Date(nextdate.toUTCString().replace(regex, ""));
return realnextdate.getTime();

But when I run it, the time is always the day before the next claim, so the remaining time is negative. Can anyone tell me how I can get the actual timestamp of the next midnight?

Edit 1: My server is in UTC + 2 timezone, if that changes anything. (Optimally, it should not.)

OIRNOIR
  • 33
  • 6

4 Answers4

1

Get time till next midnight

Algorithm

Get next midnight time as a js date object

Get the current time

Subtract, to get remaining time

  1. To get the next midnight, use
function getNextMidnightTime() {
    var midnight = new Date()
    midnight.setHours(24);
    midnight.setMinutes(0);
    midnight.setSeconds(0);
    midnight.setMilliseconds(0);

    return midnight.getTime()
}
  1. For which to get the time remaining till midnight,
function getTimeToMidnightFromNowInMS() {
    return getNextMidnight() - new Date().getTime()
}

To test

console.log(getTimeToMidnightFromNowInMS())
Nimantha
  • 6,405
  • 6
  • 28
  • 69
daniel ernest
  • 345
  • 3
  • 9
  • OP want milliseconds to UTC midnight, not Client time. – StackSlave Apr 05 '21 at 01:45
  • You can set all the time components in one go, and since you want to return a time value, then you can do: `return new Date().setHours(24,0,0,0)`. :-) – RobG Apr 05 '21 at 12:03
1

Adding one day is not as simple as adding 24 hours where daylight saving is observed, see How can I add 1 day to current date?

It's unclear to me whether you want UTC or local midnight, but here is how to get both. For UTC midnight, create a date for 24:00:00 UTC and subtract the current date and time from it.

For local midnight, subtract the date from midnight on the same day. In both cases, the milliseconds can then be formatted as H:mm:ss.sss or whatever suits.

The difference between the time to local midnight and UTC midnight will be equivalent to the current timezone offset.

// Format ms as H:mm:ss.sss
function msToHMS(ms) {
  let h = ms / 3.6e6 | 0;
  let m = (ms % 3.6e6) / 6e4 | 0;
  let s = (ms % 6e4) / 1e3 | 0;
  let ss = (ms % 1e3);
  return `${h}:${('' + m).padStart(2, '0')}:${('' + s).padStart(2, '0')}.${(''+ss).padStart(3, '0')}`;
}

// Return time to UTC midnight as H:mm:ss.sss
function timeToUTCMidnight(d = new Date()) {
  return msToHMS(new Date(d).setUTCHours(24,0,0,0) - d);
}

// Return time to local midngith as H:mm:ss.sss
function timeTolocalMidnight(d = new Date()) {
  return msToHMS(new Date(d).setHours(24,0,0,0) - d);
}

console.log('To UTC midnight  : ' + timeToUTCMidnight().padStart(12));
console.log('To local midnight: ' + timeTolocalMidnight().padStart(12));
RobG
  • 142,382
  • 31
  • 172
  • 209
0

You can get the time of the start of the next day and then subtract the current time:

function msTillNextUtcDay() {
  const msInDay = 86400000; // 24 * 60 * 60 * 1000
  const date = Date.now() // time since midnight on January 1, 1970, UTC (ecmascript epoch)
  const msNextDay = Math.ceil(date/msInDay) * msInDay; //gets the time of in ms of the start of the next utc day in ecmascript epoch "format"
  return msNextDay - date; //Subtracts current time from start of next day
}
Caleb Irwin
  • 398
  • 2
  • 5
0

You can use DateInstance.getTimezoneOffset() to get the minutes difference from the Client to UTC. Set your minutes plus the offset, after setting everything else to zero, then DateInstance.getTime() and subtract Date.now().

function msToUTCMidnight(){
  const d = new Date, z = d.getTimezoneOffset();
  d.setDate(d.getDate()+1); d.setHours(0); d.setSeconds(0); 
  d.setMilliseconds(0); d.setMinutes(0+z);
  return d.getTime()-Date.now();
}
let result = msToUTCMidnight();
console.log(result);
// show Client time at midnight UTC
console.log(new Date(Date.now()+result).toString());
StackSlave
  • 10,613
  • 2
  • 18
  • 35