I have a modal which appears once per user , and I am using local storage to achieve this. However, I am now trying to make it so that after a certain date ( 1/03/2022) to not appear at all. Here is my logic at the moment:
$(document).ready(function () {
var key = 'hadModal',
hadModal = localStorage.getItem(key);
if (!hadModal) {
$('#PIAModal').modal('show');
}
$(".btn").click(function () {
localStorage.setItem(key, true);
$("#PIAModal").modal('hide');
});
$(".modal").click(function () {
localStorage.setItem(key, true);
$("#PIAModal").modal('hide');
});
You can make a function to check if it's the due date and pass it to the conditional
function isBeforeDate() {
let today = new Date();
const endDate = new Date("2022-03-01");
if (today < endDate) {
return true
} else {
return false
}
}
if (!hadModal && isBeforeDate()) {
$('#PIAModal').modal('show');
}
You can create Date objects in Javascript and compare them.
var now = new Date();
var end = new Date("2022-03-01");
if (now < end) {
$('#PIAModal').modal('show');
}
Related
New date does not overwrite executionContext date retrived.
Hi!
I have a javascript that's calculating the end date based off the start date. The function triggers onChange for the start date field. But also before that, sets a default time for both start- and end date. This only happens when it is a create form.
The issue here is that when opening a create form, I retrieve the start and end date from the executionContext and sets the default time for both fields. Afterwards the onChange function runs, where we once again retrieves the start and end date fields from executionContext, calculates the new end date based off the retrieved start date, and makes sure that the previous end date time is set on the new end date. The time that I get is the first one caught by the executionContext and not the default time that I updated it to.
Is there some way for me to get the new value (the default time) and fetch that in the onChange function via the executionContext, without having to create a new separate js? When is the executionContext updated, because the form does get the default end time for a millisecond before getting the onChange value.
if (typeof (FA) == "undefined") { FA = {}; }
FA.Event = {
formContext: null,
OnLoad: function (executionContext) {
this.formContext = executionContext.getFormContext();
// Run on Create form
if (this.formContext.ui.getFormType() === 1) {
if (this.formContext.getAttribute("course_id").getValue() != null) {
FA.Event.SetEndDate(executionContext);
}
if (this.formContext.getAttribute("startdate").getValue() != null) {
FA.Event.SetDefaultStartTime(executionContext);
} else {
alert("startdate was null");
}
if (this.formContext.getAttribute("enddate").getValue() != null) {
FA.Event.SetDefaultEndTime(executionContext);
} else {
alert("enddate was null");
}
}
// Activates onchange events
this.formContext.getAttribute("startdate").addOnChange(FA.Event.SetEndDate);
this.formContext.getAttribute("course_id").addOnChange(FA.Event.SetEndDate);
},
SetDefaultStartTime: function (executionContext) {
var formContext = executionContext.getFormContext();
var startDate = formContext.getAttribute("startdate").getValue();
startDate.setHours(9, 30, 0);
formContext.getAttribute("startdate").setValue(startDate);
},
SetDefaultEndTime: function (executionContext) {
var formContext = executionContext.getFormContext();
var endDate = formContext.getAttribute("enddate").getValue();
endDate.setHours(17, 0, 0);
formContext.getAttribute("enddate").setValue(endDate);
},
SetEndDate: function (executionContext) {
var formContext = executionContext.getFormContext();
var startDate = formContext.getAttribute("startdate").getValue();
var endDate = formContext.getAttribute("enddate").getValue();
if (formContext.getAttribute("course_id").getValue() != null) {
// Get course
var courseId = formContext.getAttribute("course_id").getValue()[0].id;
// Get days
Xrm.WebApi.retrieveRecord("course", courseId, "?$select=days").then(
function success(result) {
console.log("Retrieved values: Days: " + result.days);
// Round days up
var days = Math.ceil(result.days * Math.pow(10, 0)) / Math.pow(10, 0);
console.log("Days rounded up where decimal result: " + days)
var newEndDate = new Date(startDate);
newEndDate.setHours(endDate.getHours(), endDate.getMinutes(), 0);
newEndDate = addDays(newEndDate, farDays);
alert("newenddate: " + newEndDate);
//sets enddate
formContext.getAttribute("enddate").setValue(newEndDate);
},
function (error) {
console.log(error.message);
// handle error conditions
}
);
}
else {
console.log("End date was not calculated.");
}
function addDays(date, days) {
var newDate = new Date(date);
newDate.setDate(date.getDate() + days);
return newDate;
}
}
}
I solved this by using global variables for start and end date, for which I set the values for from the executionContext. If the functions setDefaultEndTime or SetDetfaultStartTime runs, they update the variable for the SetEndDate function to use, otherwise original value is used.
I did not find anything about updating the executionContext or such.
Right now have I a test with an assertion that checks for today's date. However, because of time zone issues, it will start failing at a certain time of day, because the report correctly shows "tomorrow's" date while TestCafe is looking for what it has as today's date.
Basically, I would like to write an assertion that passes if it shows either today or tomorrow's date, but fail for all other values.
Is there a way to write an assertion that checks for 1 of 2 values? Is there some way to use an OR operator in an assertion?
Something along the lines of:
await t
.expect(Site.reportValues.reportHeaderInfo.innerText)
.contains({ todaysDate || tomorrowsDate },
"Report header should show either today's date or tomorrow's date",
);
I don't think this is possible. But you can do this in JS (in multiple ways, I show one, you might find a better one):
function getTodayWithOffset(offset = 0) {
let today = new Date();
today.setDate(today.getDate() + offset);
return today;
}
function isTodayOrTomorrow(date) {
return [getTodayWithOffset().toLocaleDateString(), getTodayWithOffset(1).toLocaleDateString()]
.filter(d => d === date.toLocaleDateString())
.length === 1;
}
const headerDateText = await Selector(Site.reportValues.reportHeaderInfo).innerText;
const headerDate = new Date(headerDateText); // this might be problematic, depends on what format the header text is in
await t
.expect(isTodayOrTomorrow(headerDate)).eql(true);
Those two functions could (perhaps should) go into a different file, so you keep your test file (and test case) clean with only those 4 last non-empty lines.
And when we're into testing here, you might want to check these two functions as well, it will become useful when you find a better solution later.
Using mocha and chain:
describe('isTodayOrTomorrow', function () {
it('should return true for today', function () {
expect(isTodayOrTomorrow(new Date())).to.equal(true);
});
it('should return true for tomorrow', function () {
let date = new Date();
date.setDate(date.getDate() + 1);
expect(isTodayOrTomorrow(date)).to.equal(true);
});
it('should return false for yesterday', function () {
let date = new Date();
date.setDate(date.getDate() - 1);
expect(isTodayOrTomorrow(date)).to.equal(false);
});
it('should return false for day after tomorrow', function () {
let date = new Date();
date.setDate(date.getDate() + 2);
expect(isTodayOrTomorrow(date)).to.equal(false);
});
});
describe('getTodayWithOffset', function () {
it('should return object', function () {
expect(typeof getTodayWithOffset()).to.equal('object');
});
it('should return Date object', function () {
expect(getTodayWithOffset() instanceof Date).to.equal(true);
});
it('should return today for no parameter', function () {
const today = new Date();
expect(getTodayWithOffset().toLocaleDateString()).to.equal(today.toLocaleDateString());
});
it('should return tomorrow for parameter equal 1', function () {
const date = new Date();
date.setDate(date.getDate() + 1);
expect(getTodayWithOffset(1).toLocaleDateString()).to.equal(date.toLocaleDateString());
});
it('should return yesterday for parameter equal -1', function () {
const date = new Date();
date.setDate(date.getDate() - 1);
expect(getTodayWithOffset(-1).toLocaleDateString()).to.equal(date.toLocaleDateString());
});
});
A more minimalistic solution to achieve the desired behavior could be the following:
const datesToCheckFor = ["todaysDate", "tomorrowsDate"]
await t.expect(datesToCheckFor.some(date => Site.reportValues.reportHeaderInfo.innerText.includes(date))).ok("Expecting at least one of my dates to be in there!")
I wrote the following function to try to get the user that created the gCal event.
To test this I created an event on calendar and tried to run the script. When the script executes it gets error: "TypeError: Cannot find function getCreators in object CalendarEvent. (line 68, file "Code")".
Any ideas why this is happening? I'm fairly certain getCreators() is a function in object CalendarEvent (Ref:https://developers.google.com/apps-script/reference/calendar/calendar-event#getCreators())
function getuser(instr) {
var today = new Date();
var scriptProperties=PropertiesService.getScriptProperties();
var instrcal= scriptProperties.getProperty(instr);
var event=CalendarApp.getCalendarById(instrcal).getEventsForDay(today);
if (event<1) {
var user= 'None'
}
else if (event>1) {
var user= 'Multiple Users'
}
else {
var user= event.getCreators()
}
return user
}
getEventsForDay returns an array of CalendarEvent objects, all events for that day, even if it is just one.
You'll need to go through the events to find the one you want and then call getCreators().
You can verify this by using
var event=CalendarApp.getCalendarById(instrcal).getEventsForDay(today)[0];
Here the call will work.
The method getEventsForDay(date) gets an array all events that occur on a given day.
Check that you are doing: event < 1 instead of event.length < 1.
Code:
function getuser(instr) {
var today = new Date(),
scriptProperties = PropertiesService.getScriptProperties(),
instrcal = scriptProperties.getProperty(instr),
events = CalendarApp.getCalendarById(instrcal).getEventsForDay(today),
user = 'None';
if (events.length === 1) {
user = events[0].getCreators();
} else if (events.length > 1) {
user = 'Multiple Users';
}
return user;
}
How to set date from one dateEdit to another.
I have two dateedit properties. When one dateEdit (date1) changes i need to set some value on another dateedit. I have created ondatechanged function which has some logics and then i need to set the value to date2 field. i have used js/jquery to set but the value does not bind properly after focusing or clicking the changed date2 Dateedit.
In my View
#Html.Hidden("dateTemp")
<label>R2Date</label>
#Html.DevExpress().DateEdit(
settings =>
{
settings.Name = "date1";
settings.Properties.NullText = "MM/dd/yyyy";
settings.Properties.EditFormat = EditFormat.Custom;
settings.Properties.EditFormatString = "MM/dd/yyyy";
settings.Width = System.Web.UI.WebControls.Unit.Percentage(27);
settings.Properties.ClientSideEvents.DateChanged = "OnDateChanged";
}).Bind(Model.r2date).GetHtml()
<label>RDate</label>
#Html.DevExpress().DateEdit(
settings =>
{
settings.Name = "date2";
settings.Properties.NullText = "MM/dd/yyyy";
settings.Properties.EditFormat = EditFormat.Custom;
settings.Properties.EditFormatString = "MM/dd/yyyy";
settings.Width = System.Web.UI.WebControls.Unit.Percentage(27);
settings.Properties.ClientSideEvents.DateChanged = "ReportOnDateChanged";
}).Bind(Model.date1).GetHtml()
[JScript]
function OnDateChanged(s, e) {
var dateVal = s.GetText();
//my logic here
dateOnchange();
}
dateOnchange(){
//my logic here just need to call reportondatechange()
ReportOnDateChanged();
}
function ReportOnDateChanged(s,e )
{
dateVal1 = $("#dateTemp").val(); //dateval1 has some values here
s.SetDate(dateVal1);//not working how to set the value here
}
https://documentation.devexpress.com/#AspNet/DevExpressWebScriptsASPxClientControl_GetControlCollectiontopic
This should do it
var editor = ASPxClientControl.GetControlCollection().GetByName("date2");
if (editor) {
editor.SetValue(dateVal1);
}
$("#date2").val(dateVal1);
This is my coding from javascript but it did't work
if ( $("#requestDueDate").val().length < CurDate ) {
alert("Due date should be more than current date");
setBoolean = false;
return false;
}
Thank you.
Convert the string to a date:
// Expect d/m/yyyy
function toDate(s) {
s = s.split(/\D/g);
return new Date(s[2],--s[1],s[0]);
}
Then compare to the current date:
function afterNow(s) {
var now = new Date();
return +toDate(s) > +now;
}
Try it:
afterNow('9/10/2013'); // false
afterNow('9/10/2031'); // true