Get the next upcoming Friday at 5 PM - momentjs

I need to display how far away a deadline is that is every Friday at 5PM. Moment will give me that date for a day of the week, but it's always the current time, so the deadline always displays 'x days, 23 hours, 59 minutes'. How do I get the day, AND a specific time from moment? In my example, I need deadline to be 'next Friday, at 17:00' instead of 'next Friday, at the current time'
console.log(timeLeft());
function timeLeft() {
var dayINeed = 5; // for Friday
var deadline;
// if we haven't yet passed the day of the week that I need:
if (moment().isoWeekday() <= dayINeed) {
// then just give me this week's instance of that day
deadline = moment().isoWeekday(dayINeed);
} else {
// otherwise, give me next week's instance of that day
deadline = moment().add(1, 'weeks').isoWeekday(dayINeed);
}
console.log(deadline);
const now = moment();
const days = deadline.diff(now, 'days');
const hours = deadline.subtract(days, 'days').diff(now, 'hours');
const minutes = deadline.subtract(hours, 'hours').diff(now, 'minutes');
return `${days} days, ${hours} hours, and ${minutes} minutes`;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>

You have to set to deadline the time (17:00), instead of getting current time, you can use moment startOf() and set(), simply add the following to your code:
deadline.startOf('day').set({h: 17});
This way you are setting 17:00:00 to deadline and you will get the desired output.
Here a full example:
console.log(timeLeft());
function timeLeft() {
var dayINeed = 5; // for Friday
var deadline;
// if we haven't yet passed the day of the week that I need:
if (moment().isoWeekday() <= dayINeed) {
// then just give me this week's instance of that day
deadline = moment().isoWeekday(dayINeed);
} else {
// otherwise, give me next week's instance of that day
deadline = moment().add(1, 'weeks').isoWeekday(dayINeed);
}
deadline.startOf('day').set({h: 17});
console.log(deadline.format());
const now = moment();
const days = deadline.diff(now, 'days');
const hours = deadline.subtract(days, 'days').diff(now, 'hours');
const minutes = deadline.subtract(hours, 'hours').diff(now, 'minutes');
return `${days} days, ${hours} hours, and ${minutes} minutes`;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
If you want, you can use setter like hour(), minute() etc to set 17:00:00 to deadline.

Related

How can I Set Week which starts from Friday and Ends on Next Thursday in DaterangePicker

I am Using JQuery DateRangePicker by Dan Grossman, right now I Configured it to enable Current Week's Dates only by momentjs.
Here is my script
var startDate = moment().startOf('week').toDate();
var endDate = moment().endOf('week').toDate();
$(document).ready(function () {
$("#paymentDateTimePicker").daterangepicker({
singleDatePicker: true,
autoUpdateInput: false,
minDate: startDate,
maxDate: endDate,
locale: {
format: 'MM/DD/YYYY',
firstDay: 1
}
});
});
By default, this configuration starts from Sunday to Saturday.
Here is the output.
Now I want to Configure this dateRangePicker to starts on Friday instead of Sunday and ends on Thursday.
OR We can say From 2nd Of OCT to 8th of OCT.
I tried the add() and subtract() Methods of momentjs but can not find a proper solution to this.
Help me with this.
Thanks for your help, Above solution will work on regular days but if Today's Saturday then
it will not work.
here is a possible solution.
var day = moment().day();
var startDate;
var endDate;
if (day >= 5) {
startDate = moment().day(5).toDate();
endDate = moment().day(5).add(6, 'days').toDate()
}
else {
startDate = moment().startOf('week').subtract(2, 'days').toDate();
endDate = moment().endOf('week').subtract(2, 'days').toDate();
}
or can also use Datejs for easy chaining.
You can Modify as the date range from the Week Like This
var startDate = moment().startOf('week').subtract(2,'days').toDate();
var endDate = moment().endOf('week').subtract(2, 'days').toDate();

Why is the end date for one more day?

select: function(start, end) {
var formatStart = start.format();
var formatEnd = end.format();
alert(formatEnd);
showProjModal(formatStart,formatEnd);
},
i choice 2016-08-11 to 2016-08-13,but formatEnd is 2016-08-14,i do not know why?
From the docs at fullcalendar.io.
end is a Moment indicating the end of the selection. It is an exclusive value, so if the selection is all-day, and the last day is a Thursday, end will be Friday.
I guess your selection is all day? So it is working as intended.
You can check this by calling hasTime
Example
if (end.hasTime()) {
// Specific endpoint. For example 2016-07-13 10:00:00'
}
else {
// All day. For example 2016-07-13
// If you want to output the end day you selected here subtract 1 of the day
end.subtract(1, 'days');
}

Meteor : Countdown timer

This might be a big ask, but I'm completely stuck so any help is appreciated.
I'm trying to create a countdown timer that runs from Sunday to Sunday and just restarts at the end of the week. I've tried using countdown packages in atmosphere but the documentation is limited and never seems to work. I've also tried to download and run 3rd party jquery packages however they always seem to crash meteor.
Could someone point me in the right direction or show me how to do this in meteor?
Specific details:
Countdown timer used to run an auction.
Auction runs for 7 days, Starts Sunday at 12:00am finishes 7 days
later.
Auction resets and starts again after 7 days.
Countdown timer will be visible by users on multiple pages.
Countdown timer units to be displayed - Days, Hours, Minutes, Seconds (eg.
6 days, 3 hours, 55 minutes, 22 seconds until the next auction
begins.)
The question is too large. But i can suggest the small step to work with this. Your auction scheme will need to have a endDateTime to store the value (even it will start/end in Sunday). On the template you need to display the timer, set one ReactiveVar as number (to count down), one ReactiveVar as string (to display to result)
Template['countDownTemplate'].created = function() {
var due, dueDate, duration, now, num, self;
self = this;
dueDate = Template.instance().data['auction']['endDateTime'];
now = moment.utc();
due = moment.utc(dueDate, 'YYYY-MM-DD hh:mm:ss');
duration = moment.duration(due.diff(now));
num = Math.floor(duration.asSeconds());
if (num >= 0) {
self['remaining'] = new ReactiveVar<number>(num);
self['timeRemaining'] = new ReactiveVar<string>(convertPeriod(num));
self['interval'] = Meteor.setInterval((function() {
var remaining;
remaining = self['remaining'].get();
self['remaining'].set(remaining - 1);
self['timeRemaining'].set(convertPeriod(self['remaining'].get()));
if (remaining === 0) {
Meteor.clearInterval(self['interval']);
} else {
remaining = Math.floor(moment.duration(due.diff(now)).asSeconds());
}
}), 1000);
}
};
(the convertPeriod will be based on the remaining number to convert into your correct format)
The rest is just about showing timeRemaining in the correct format with the convertPeriod

node RRule check if rule day is today

I am using 'rrule.js' library in node to parse my RRule. I would like to know if current day is same as rule day. It works on most cases but not all. I also use moment.js to compare. The issue is in "rule.after()". It should include the current day but it doesn't.
function checkIfToday(rruleStr){
var RRule = require('rrule').RRule;
var moment = require('moment');
var rule = RRule.fromString(rruleStr);
// Convert all dates into UTC before comparison
var todayutc = moment().utc(); // today in UTC
var nextOccurrence = rule.after(todayutc,inc=true); // next rule date including today
var nextOccurutc = moment(nextOccurrence).utc(); // convert today into utc
var match = moment(nextOccurutc).isSame(todayutc, 'day'); // check if 'DAY' is same
return match;
}
Any idea what's the best way to do this.
Thanks.
This worked for me. Try setting the time of todayutc back to the beginning of the day using moment's startOf method:
function checkIfToday(rruleStr){
var RRule = require('rrule').RRule;
var moment = require('moment');
var rule = RRule.fromString(rruleStr);
// Convert all dates into UTC before comparison
var todayutc = moment().utc().startOf('day'); // today in UTC
var nextOccurrence = rule.after(todayutc, true); // next rule date including today
var nextOccurutc = moment(nextOccurrence).utc(); // convert today into utc
var match = moment(nextOccurutc).isSame(todayutc, 'day'); // check if 'DAY' is same
return match;
}

Something Like setAsToday(date)?

With default view set to agendaWeek. If I load fullCalendar at 11:59PM the today day slot is not automatically updated after 12:00AM until I refresh the browser.
This is how I fixed it:
Call a function every 5 minutes (or any interval that suits your requirements)
$(function() {
var tInterval = 5*60*1000;
timelineInterval = window.setInterval(updateFcToday, tInterval); // Update timeline after every 5 minutes
});
Add a function like this to the page that contains your calendar:
function updateFcToday() {
var curTime = new Date();
if(curTime.getHours() == 0 && curTime.getMinutes() <= 5) // this five minutes is same interval that we are calling this function for. Both should be the same
{// the day has changed
var todayElem = $(".fc-today");
todayElem.removeClass("fc-today");
todayElem.removeClass("fc-state-highlight");
todayElem.next().addClass("fc-today");
todayElem.next().addClass("fc-state-highlight");
}
}

Resources