Reputation: 89
I want to get hours difference between now and next friday 18:00 (weekend) using momentjs, I tried this but it doesn't work :
public getWeekend() {
const nextFriday = moment().weekday(4)
nextFriday.hours(18) // Weekend time: 18:00
nextFriday.minutes(0)
nextFriday.seconds(0)
const diff = moment(nextFriday.diff(moment()))
console.log(diff.days())
if (diff.days() * 24 + diff.hours() <= 99) {
return nextFriday.toString()
} else {
return 0
}
}
}
console.log(diff.days()) is printing 6 days whereas we are Wednesday
Upvotes: 1
Views: 108
Reputation: 1609
You can take hours difference using (now - next Friday) / 60*60*1000
method, Try it:
const nextFriday = moment().day(5)
var hours = Math.abs(new Date() - nextFriday) / 36e5;
console.log(hours);
Note: 36e5
is the scientific notation for 60*60*1000
Upvotes: 0
Reputation: 7455
UPDATED , it is totally correct now.
First of all Friday actually has sequence number 5
, so use it instead 4.
const nextFriday = moment().weekday(5)
- this is correct Friday.
Next important thing is that nearest Friday is taken by default, it means that already passed Friday can be selected (for example if it is Satturday today). In such case we just need to add one week.
Here is working code snippet:
function getWeekend() {
const today = moment();
const friday = moment().weekday(5);
// check if wee need to add one week in case of previous Friday selected
if (friday < today) {
friday.add(1, 'weeks');
}
friday.hours(18) // Weekend time: 18:00
friday.minutes(0)
friday.seconds(0)
return friday.diff(moment(), 'hours');
}
const hoursToFriday = getWeekend();
console.log(`Friday 18:00 will be in ${hoursToFriday} hours`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
Upvotes: 0
Reputation: 11
Moment.js has a build in function .fromNow() it returns the time beween the given time and the local time. So the only 2 line you need to change are the .weekday(4) because friday is weekday(5). and call the .fromnow() function.
console.log(getWeekend());
function getWeekend() {
const nextFriday = moment().weekday(5);
nextFriday.hours(18); // Weekend time: 18:00
nextFriday.minutes(0);
nextFriday.seconds(0);
return nextFriday.fromNow();
}
<script src="https://momentjs.com/downloads/moment.min.js"></script>
Note that fromNow will return a string Moment.js has a build in solution for this.
Upvotes: 0
Reputation: 4806
try this:
function getWeekend() {
const nextFriday = moment().weekday(4)
nextFriday.hours(18) // Weekend time: 18:00
nextFriday.minutes(0)
nextFriday.seconds(0)
const diffHours = moment.duration(nextFriday.diff(moment())).asHours()
console.log(`${diffHours} hours`) // 31.7 hours
if (diffHours <= 99) {
return nextFriday.toString()
} else {
return 0
}
}
Upvotes: 1
Reputation: 13892
https://jsfiddle.net/7ehoq06n/
If you just call moment().weekday(5)
it will go to Friday of "this week", which means that if you're on a saturday, it will go to YESTERDAY.
If you want the next friday, always in the future, use this:
var today = moment();
var friday = moment().weekday(5);
if (friday < today) {
friday.add(1, 'weeks');
}
Upvotes: 0
Reputation: 4023
I recommended to use moment
const diff = moment.duration(nextFriday.diff(moment()))
const deepDiff = diff.format("D [day], H [hour and] m [min]")
Upvotes: 1