简体   繁体   中英

javascript - get the date of next day in week?

how do I get, for example, the date of next monday and the time 5:30PM, and calculate the difference between current date and time and that date and time?

if I run it now at 8/28/2020 17:35, it should give me 8/31/2020 17:30 and the difference 2 days 23 hours 55 minutes.

I hope this help:

 // takes dayIndex from Sunday(0) to Saturday(6) const getNextDay = (dayIndex) => { const today = new Date(); today.setDate( today.getDate() + ((dayIndex - 1 - today.getDay() + 7) % 7) + 1 ); today.setHours(17, 30, 00); return today; }; const getTimeleft = (dateNow, dateFuture) => { let seconds = Math.floor((dateFuture - dateNow) / 1000); let minutes = Math.floor(seconds / 60); let hours = Math.floor(minutes / 60); let days = Math.floor(hours / 24); hours = hours - days * 24; minutes = minutes - days * 24 * 60 - hours * 60; seconds = seconds - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60; return `${days} days ${hours} hours ${minutes} minutes`; }; const now = new Date(); const nextMonday = getNextDay(1); const timeleft = getTimeleft(now, nextMonday); console.log(nextMonday.toLocaleString()); console.log(timeleft);

You could use moment.js, it's a very useful library when it comes to dates:

 <script src="https://momentjs.com/downloads/moment.js"></script> <script> const today = moment(); const nextMonday = moment().add(1, 'weeks').isoWeekday(1); nextMonday.set({'hour': 17, 'minute': 30, 'seconds': 0}); console.log(nextMonday.toString()); const duration = moment.duration(nextMonday.diff(today)); const days = duration.asDays(); const hours = (days - Math.floor(days)) * 24; const minutes = (hours - Math.floor(hours)) * 60; console.log("days", Math.floor(days)); console.log("hours", Math.floor(hours)); console.log("minutes", Math.floor(minutes)); </script>

Here is the working example:

 function nextWeekMonday(date) { var diff = date.getDate() - date.getDay() + (date.getDay() === 0 ? -6 : 1); var currWeekMonday = new Date(date.setDate(diff)); return new Date(currWeekMonday.getTime() + 7 * 24 * 60 * 60 * 1000); } function getDateDifference(current, future) { // get total seconds between the times var delta = Math.abs(future - current) / 1000; // calculate (and subtract) whole days var days = Math.floor(delta / 86400); delta -= days * 86400; // calculate (and subtract) whole hours var hours = Math.floor(delta / 3600) % 24; delta -= hours * 3600; // calculate (and subtract) whole minutes var minutes = Math.floor(delta / 60) % 60; delta -= minutes * 60; // what's left is seconds var seconds = delta % 60; return `${days} Days, ${hours} Hours, ${minutes} Minutes, ${seconds} Seconds`; } var curr = new Date; // get current date var nextMonday = nextWeekMonday(curr); console.log(getDateDifference(curr, nextMonday));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM