How exclude especific days in Watson conversation - ibm-watson

I'm doing a service chat and I'd like to extract days that correspond to Sunday. For exemple:
Watson: Choose a date
user: day 30
Watson: We don't open this day, because it's a Sunday.
The problem is.. the Sundays of this month are not the same as next month.. I tried with an array inside the context with Sundays days of this month(ex: "2","9","16","23","30"), but the Conversation didn't understand that day 30 is a Sunday.. Could anyone help me please?
Tks! :)

In this case, unfortunally, #sys-date does not work equal #sys-number.
In this case with #sys-number, if user type 1, #sys-number recognize and can use #sys-number:1 with condition inside the flow.
Unfortunally, #sys-date doesn't.
In this case, for get the date with javascript you can use:
new Date() //get the date NOW
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
You can see the now() is the same format to get date:
new Date(year, month, day, hours, minutes, seconds, milliseconds)
And you can convert the #sys-date to the same format, and use this to verify the name of the date.
Get the day with context variable and use code in your application. For example:
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var dateObject = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
var dayName = days[dateObject.getDay()];
console.log(dayName);
And make some condition... for example:
if(dayName === 'Sunday'){
data.output.text[0] = "We don't open this day, because it's a Sunday."
}
I code this and works fine, see:
function dayRequest(data, req, res){
console.log('works the true condition context')
var dateString = data.context.day; //context variable with <? #sys-date ?>
var dateParts = dateString.split("/");
var dateObject = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var dateObject = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
var dayName = days[dateObject.getDay()];
console.log(dayName);
if(dayName === 'Sunday'){
data.output.text[0] = "We don't open this day, because it's a Sunday.";
return res.json(data);
}
}
Check my app:
In this case, you can use now to get the date and in your application you'll end a message if the day is Sunday... You can use context variable for this too, but, you need set all dates.. And with this code, you'll check the date and your application will send a message.
Summary: convert your #sys-date to the same format now() and use my code to send a message for your user if the day is Sunday. I created one context variable with to save the date after request and in the next flow I create one context with dayTrue: true inside conversation flow. In my application, I created one condition in my updateMessage() if data.context.date === true, my function dayRequest() will execute.

Related

Calculate Day of a week for current year from Moment

I am making a Alarm System for Mon, Tue, Wed, Thu, Fri Sat and Sun separate. Let's say i am setting an alarm for Tuesday than i need all Tuesdays of current Month and atleast for current year till the alarm is Off. I want to achieve it using moment.
I am using Package - 'moment-weekdaysin', this is not giving me proper result. It is giving me incorrect date.
code - moment().weekdaysInMonth('Monday')
I've not used moment-weedaysin before but it should share most of the same API as core moment. The code below will capture all Tuesday dates in the format of Month-Day-Year (MM-DD-YYY) from the current date til the end of the current year. You can change the parameter for any other days 1-7 (Monday-Sunday).
import moment from 'moment';
const getOccurrencesOfDayThisYear = (day = 1) => {
let startDate = moment();
const endOfYear = moment().endOf('year');
const extractedDates = [];
while (startDate.isBefore(endOfYear)) {
if (moment(startDate).day() == day) {
extractedDates.push(moment(startDate).format('MM-DD-YYYY'));
}
startDate = moment(startDate).add(1, 'days');
}
return extractedDates;
};
// 2 represents Tuesday; 1 being Monday and 7 being Sunday
getOccurrencesOfDayThisYear(2)
There might be a more sophisticated way of performing this through various moment methods.

Calculate the number of days between selected date and current date using datetime-picker AngularJs

I'm using bootstrap datetime-picker in angularjs
I disabled few days using the option dateDisabled of the date picker like this
$scope.dateOptions = {
dateFormat: 'yyyy-MM-dd',
maxDate: new Date(2020, 5, 22),
minDate: new Date(),
startingDay: 1,
onChangeDate: countDays,
dateDisabled: function (data) {
var date = new Date(data.date)
date.setHours(0, 0, 0, 0)
var date2 = new Date('2019-02-08')
date2.setHours(0, 0, 0, 0)
return (date == date2.toString());
}
};
Now need to calculate the number of days between the selected date and the current date based on the date picker i.e. disabled date should not be count in the days calcluation.
If i selected the date as 10 feb 2019 then the number of days to be count as
4 (using current date - 5 feb 2019 ).
but it is coming as 5
The function get calls when i select the date from date picker
function countDays(dateTime) {
var fourDaysLater = new Date();
fourDaysLater.setDate(dateTime.getDate() - 4);
}
How to count dates which are enabled in the date picker?
Answering your question
How to count dates which are enabled in the date picker?
You have disabled the dates using date picker but when you are counting the number of days between two dates you are using new date() which can't access your date picker's date.
You can do something like this -
function countDays(dateTime) {
// Current date
var currentDate = new Date().setHours(0, 0, 0, 0);
// Selected date
var selectedDate = new Date(dateTime).setHours(0, 0, 0, 0);
// Count working days between selected date and current date
while (currentDate < selectedDate) {
if (currentDate != new Date('2019-02-08').setHours(0, 0, 0, 0)) {
++workingDays;
}
currentDate.setDate(currentDate.getDate() + 1);
}
}
alert(workingDays); // Number of working days
Count the number of days between two days and exculde the

Reactjs DayPickerInput disabled days

I have a DayPickerInput element from react-day-picker plugin and I don't know how to disable all days after a month(31 days) starting with current day. Any help please?
Thanks.
The documentation could be a clearer. This should do it for you:
<DayPickerInput
value={moment(minDate).format('YYYY-MM-DD')}
dayPickerProps={{
disabledDays: {
after: new Date(2018, 3, 20),
},
}}
onDayChange={day => console.log(day)}
/>
Replace the new Date(y, m, d) with your date.
[Edit per my comment]
Not all months are 31 days, if you literally want to add 31 days to the first of a month:
Source: Add day(s) to a Date object
var myDate = new Date();
myDate.setDate(myDate.getDate() + AddDaysHere);
Where "AddDaysHere" would be 31.
If you just want to insure there is no way to select a date next month, you could:
// There is probably a billion better ways to get the next available month, this is just basic
let currentMonth = 2;
let nextMonth = currentMonth + 1;
if (nextMonth > 11) { nextMonth = 0;} // I believe javascript months start at 0.
Date(2018, nextMonth, 1)
Happy coding!

Convert first day of month to Last day of month in Angular JS

I am very new to angular JS but since morning struggling with this.
I have a datepicker with "MM/yyyy" format, the date value returned here is first day of month.
i.e. February 1, 2017 but i want the date as February 28, 2017 i.e last day of month.
Just to update i am using moment function.
Please suggest some work around for the same!
I infer from your question that you are using momentjs.
This lib provides you with a built in function endof
const date = new Date(2017, 1) // 1st Feb
moment(date).endOf('month');
This should handle most cases directly including leap years
If you have a JavaScript Date instance d, you can simply use
const d = new Date(2017, 1) // 1st Feb
d.setMonth(d.getMonth() + 1)
d.setDate(d.getDate() - 1)
console.info(d.toLocaleString())
Now d will be the last day of the month.
Note: this easily handles year boundaries without any extra code. For example
const d = new Date(2017, 11) // 1st Dec
console.info('Before', d.toLocaleString())
d.setMonth(d.getMonth() + 1)
d.setDate(d.getDate() - 1)
console.info('After', d.toLocaleString())
You just need add 1 month to the date, and then substract 1 day. Here's an example:
// Let's suppose the date selected from the picker was February 1st, 2017
// Remember months in JS are zero-index based, so 0=Jan, 1=Feb, etc.
var selectedDate = new Date(2017, 1, 1);
var month = selectedDate.getMonth();
var year = selectedDate.getFullYear();
month ++;
if(month > 11){ // Last month number is 11 (December)
month = 0; // January
year ++;
}
var oneMonthAheadDate = new Date(year, month, 1);
var lastDayOfSelectedMonthDate = new Date(oneMonthAheadDate.getTime() - (1000 * 3600 * 24)); // We substract 1 day (1000 ms x 3600 secs in an hour x 24 hours in a day)
Your needed date will be in lastDayOfSelectedMonthDate

Angularjs Django compare dates

I am fetching the date from my Django backend which comes like this: 2016-03-31
In my angular controller I want to compare it with today's date and if they are similar I want to deactivate a button.
I tried new Date() which gives me something like Thu Mar 31 2016 08:59:01 GMT+0200 (W. Europe Daylight Time)
How can these two dates be compared to achieve my goal?
ehhhhh... i think currently we could only do a manual formatting
see this one: How to format a JavaScript date
For your reference, below is what i did though:
$scope.formatDate = function(date){
var newDate = new Date(date);
var year = newDate.getFullYear();
var month = (newDate.getMonth() + 1).toString(); //add 1 as Jan is '0'
var day = newDate.getDate().toString();
month = month.length > 1? month: '0'+month;
day = day.length > 1? day: '0'+day;
$scope.date = day + '/' + month + '/' + year;
}
If you want to compare the date with now you can do the following:
var now = new Date().getTime();
var compareDate = new Date('2016-03-31').getTime();
if(now > compareDate){
//greater
}else if(now < compareDate){
//less
}else{
//equal
}
Just add what you need to the scope and then you could do something like:
ng-disabled="compareDate === today"

Resources