Function with If/Else Statement - Comparing Dates - javascript

I am working with a function and I am trying to write an if-else statment that compares a textfield aganist some dates.
This is what my function looks like right now:
function updateSwitch(){
if (effDate.getValue() > effDate.getValue(new Date())){
submitButton.disable();
}
else{
submitButton.enable();
}
}
I am trying to compare the dates among the same textfield. I am trying to say that if the date enetered in the textfield is in the future (any day after tomorrow), then disable the submitButton. I know that
the function works if I compare the textfield aganist a different textfield, but I cannot get it to work within the same textfield.

I'm not sure how you have your dates constructed. Is effdate been constructed with the Date object? If so, may I suggest using valueOf(). It'll return the primitive value - or the date in milliseconds. This should allow for a more consistent comparison.
function updateSwitch(date){
var effDate = Date.parse(date);
var curDate = new Date();
if (effDate.valueOf() > curDate){
submitButton.disable();
}
else{
submitButton.enable();
}
}

Related

How to check in AngularJs if some dates are greater then others

I need to check if some date fields are greater then others.
I solved in this way for only one couple of dates. I can't find a solution for multiples couples of variables like these:
$scope.date1startFrom;
$scope.date1startTo;
$scope.date2startFrom;
$scope.date2startTo;
$scope.date3startFrom;
$scope.date3startTo;
I have to see the alert only if very element of the couple has a value. I find this solution but I want to extend it to multiple values but I can't.
var datesAreDefined = $scope.date1startFrom && $scope.date1startTo;
if (datesAreDefined && !periodFromAndToIsValid($scope.date1startFrom, $scope.date1startTo)) {
alert("error");
}
function periodFromAndToIsValid (from, to){
var fromDate = new Date(from).getTime();
var toDate = new Date(to).getTime();
return toDate >= fromDate;
}
I would recomend to use something like MomentJS its very effective for this case:
https://momentjs.com/
In this link you can find the way to make comparisons easily between dates.
https://momentjs.com/docs/#/query/

Google Scripts - "If" comparison not working

I want to protect the document from other users if a date in Column B1 + 2days is greater than today.
I had no problem with protection part, but I can't get the IF statements to work for some weird reason I can't understand.
This is the script where i compare dates:
ss = SpreadsheetApp.getActiveSheet();
send_date = ss.getRange("B1").getValue();
limit = new Date(send_date.setDate(send_date.getDate()-2))
limit.setHours(0,0,0,0)
day0 = new Date();
day0.setHours(0,0,0,0)
ss.getRange("D1").setValue(day0);
ss.getRange("D2").setValue(limit);
ss.getRange("D3").setValue(limit>day0);
ss.getRange("D4").setValue(limit<=day0);
if (day0>limit) { ss.getRange("C1").setValue("can be edited");}
else if (day0<=limit) { ss.getRange("C1").setValue("cannot be edited");}
I set hours/minutes/seconds etc to 0 because I only need to compare the dates day by day.
What happens after I run the script?
For example, if B1 = '2017-10-24', D1 sets to today ('2017-10-26'), D2 sets to ('2017-10-22').
D3, with limit>day0 comparison gets value FALSE, D4 limit<=day0 gets TRUE.
So far so good, but when it comes to IF statements, it seems that these comparisons are reversed.
In this example, C1 is set to 'can be edited'.
For me, that means that first comparison return FALSE, the second returned TRUE.
When I set date to a date in future (i.e. '2017-10-30'), D1-D4 fields get the right values, but C1 is set to 'cannot be edited'.
I'll be grateful for any help.
You can not compare two objects (day0 and limit) like that.
But, you can compare a value of those two objects. For example, using getTime().
var day0Time = day0.getTime();
var limitTime = limit.getTime();
if (day0Time > limitTime) {
// do stuff
}

HTML Comparing 2 Dates with Javascript

I'm trying to compare 2 dates using Javascript. If "myDateL" is after "mydateR" display a message box when the button is clicked.
What is wrong with my code?
I know I've seen a similar thread to this but I couldn't understand it. I hope someone can help me with this please.
<input type="Button" value="TwoDates" onClick="twoDates()">
<script>
function twoDates() {
var firstdate = new date(document.getElementById("mydateL").value);
var seconddate = new date(document.getElementById("mydateR").value);
if(firstdate > seconddate) {
alert('Please change your return date.');
}
}
</script>
It's new Date(...), not new date(...). Date is the global object that holds dates and times, date would be a function you've declared called date. If you look at the console when you run this, you should see something like:
ReferenceError: date is not defined

Detect SQL min date string in javascript

In an angularJS application I'm working on, we are using a SQL server to provide data. Nothing odd there. I am tasked with working on the date sanitation, so objects that are passed into our application that have the SQL min date value are undefined, and ones that do have a date are javascript Date objects.
I know that during upgrades, sometimes the string that is defined for min date can change. Right now, it is 0001-01-01T00:00:00Z. I cannot just do a string comparison because it could change.
My problem is, how do i determine this min date in javascript? new Date(varThatIsMinDate) is a valid date. MomentJS thinks its valid too, and it is technically a valid date. I'm less worried about validation and more worried about "is it min date"
How do you determine min date === true from a zulu pattern 0001-01-01T00:00:00Z or similar in javascript?
My code so far, cause I know you are gonna ask for it:
if (response.config.responseType === 'json') {
for (var property in response.data) {
if (response.data.hasOwnProperty(property)) {
if (property.toUpperCase().indexOf('DATE') > -1 || property.toUpperCase().indexOf('TIME') > -1) {
console.log(property.toUpperCase());
// Attempt to use JS built in date and validate
var tmpDate = new Date(response.data[property]);
if ($moment(tmpDate).isValid()) {
// Make it a valid date object if it has a valid date
response.data[property] = tmpDate;
} else {
// Make it undefined
response.data[property] = undefined;
}
}
}
}
}
Thing is, its always valid, so this code doesn't work.
EDIT: I could cheat and use tmpDate.getUTCFullYear() === 1 but i'd like to know how to do this the right way.

HTML passing parameters to javascript function that then does a date compare and then javascript returning a result to be displayed in html

I am having problems figuring out how to do a date compare in Javascript, and how to pass and receive varialbes to and from Javascript Function and Html.
I have 4 term dates (workshop start and end dates for each term). I will send one set of dates at a time to the javascript function, and then I want the Javascript to check if today's date falls within the range of that workshop. Then the javascript should return a value like "workshop finished", "workshop in progress", "workshop to come" (depending on wheiter todays date is before, during, or after that specific date range). I'll call the function 4 different times - each with a different range of dates.
So for example, If the first set of dates are: 6th February till 13th March 2014, then how would I call the javascript function? This is what I have so far:
In the html - at the point where I want the status to be displayed, I tried this:
<script language="JavaScript" type="text/javascript">
<!--//
document.write(displayStatus(02062014, 03132014));
//-->
</script>
<noscript></noscript>
I know that the above date formating is probably wrong that I am trying to send to the function, but I am not sure what format is needed to do a date compare. (Note: the empty noscript tags are because if scripting is turned off, I don't want anything displayed.)
I've started the function like this (found inside status-date.js file):
function displayStatus(fromDate,toDate) {
todayDate = new Date();
// this is a comment for code that needs to be written:
//
// magic happens here that compares today's date to the fromDate and toDate
// and returns th appropriate status line
//
// the rest below is how I think the status is returned to the html
// (I'm using 'in progress' as a test example)
var dateStatus = "in progress"
return dateStatus;
}
Note: the function is loaded in from the head section as follows:
<script src="assets/js/status-date.js" language="javascript" type="text/javascript"></script>
Even with this simple function.. that basically gives me the status without any calculations, just so I can test the comunication between HTML and function.. it doesn't display anything. So I can't really move forward until I figure out this basic step first.
So, could some kind sole help me figure this out.
How to properly send formated date parameters to the javascript function.
How to display the status results that need to be returned to the html
How do you (inside the function) check if todays date falls within a range of provided dates.
Thanks for any help provided.
SunnyOz
I believe that you are looking for something like this...
function displayStatus(fromDate,toDate) {
var status;
var today = new Date();
today = Date.parse(today);
if(today >= fromDate && today <= toDate) {
status = 'In Progress';
} else if(today > toDate) {
status = 'Workshop Finished';
} else {
status = 'Workshop To Come';
}
return status;
}
<script>
window.onload = function(){
var startDate = new Date(2014,1,6); //6th February
var endDate = new Date(2014,2,13); //13th March 2014
document.write(displayStatus(Date.parse(startDate), Date.parse(endDate)));
};
</script>
Here are some other helpful resources:
Compare two dates with JavaScript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Categories

Resources