IF THEN using a Timestamp - javascript

Do IF statements work using timestamps? I'm using a Google Sheet from a Google Form and would like to use the Timestamp as a matching identifier.
Code:
if (time == homeTime) {
s.getRange(7, 1).setValue("Yay!");
Logger.log shows both variables are indeed the same value (Tue Jun 26 08:47:52 GMT-04:00 2018).
It works if I choose a value other than the timestamp, such as an email address.
I cannot use < or > because I'm using this in a for loop. As a temporary fix, I changed the format of the column to Plain Text using #, but time - homeTime == 0 is the answer I needed!
I'm trying to figure out how to mark my answer correctly so I apologize if I mess it up.

Related

Jquery script doesn't work so I have to fix the code

I would like to disable some fields on a select box, depending on the date and time. I have a WooCommerce website. On my checkout page, I have a select box within a list of specific dates and times when I deliver my product. I would like to prevent visitors from scheduling a delivery for the same day that they place an order. My delivery days are Wed 10:00 - 12:00, Wed 16:00 - 18:00, Sat 10:00 - 12:00, and Sat 12:00 - 14:00.
If a visitor goes to checkout on Wed, I want prevent them from choosing a Wed delivery date (by disabling Wed on the select box), so they can choose only between Sat 10:00 - 12:00 and Sat 12:00 - 14:00. The same is true for Sat.
I am trying to write some jQuery code, but it isn't working. Please, could someone help me to fix this code?
Checkout page link
jQuery(document).ready(function() {
var dmain = new Date(); // for now
dayis = dmain.getDay();
houris = dmain.getHours();
if(dayis == 3 && houris >= 8){
jQuery("#billing_woocmm9 option[value='mercoledì 10:00 - 12:00']").hide();
jQuery("#billing_woocmm9 option[value='mercoledì 16:00 - 18:00']").hide();
}
if(dayis == 6 && houris >= 7){
jQuery("#billing_woocmm9 option[value='sabato 10:00 - 12.00']").hide();
jQuery("#billing_woocmm9 option[value='sabato 12:00 - 14:00']").hide();
}
Your help will be really appreciated. Thank you so much!
As that comment says, there is a missing } at the end. Also, while we're at it, "let" is a better statement than "var" in this situation because "var" will declare the variable globally, whereas "let" will restrict it to this small scope. Also, the variable names shouldn't really be suffixed by "is" at the end because the == operator does that for you. Also, I don't think the hide() method can be used on "option" elements, so you should use the remove() method instead. Also, you might want to check that those values for your options are actually what are used by the site. Typically, option values are a condensed, non-verbose, abbreviation for what is selected, so that could also be the problem. Anyway, here's what might work for you:
jQuery(document).ready(function() {
let now = new Date();
let day = now.getDay();
if (day == 3) {
jQuery("#billing_woocmm9 option[value^='mercoledì']").remove();
}
else if (day == 6) {
jQuery("#billing_woocmm9 option[value^='sabato']").remove();
}
}
And here is an alternative method that uses dictionary lookups to avoid a little bit of code repetition:
jQuery(document).ready(function() {
let now = new Date();
let day = now.getDay();
let day_value_prefixes = {
3: "mercoled",
6: "sabato",
};
if (day_value_prefixes.hasOwnProperty(day)) {
jQuery("#billing_woocmm9 option[value^='"+day_value_prefixes[day]+"']").remove();
}
}
Note that in both of these methods, I used "value^=". The ^= operator here selects values that start with the string, so that we can ignore the ending of the string with all the spaces. Generally, I like to avoid selecting based on weird characters like that to avoid any problems that might arise from unnoticed double spaces or anything. Also, along those same lines, I generally don't like to use non-ascii characters like ì internally, in non-user-facing situations, in case there is some weird encoding problem that causes them not to match with selectors or if there is some very similar-looking but technically different character that I would get them confused with.
If you are still having problems, it would be helpful if you would post the relevant html. It could be that your selector is off somehow - maybe you are using the wrong id for your select tag or something. You might want to also check that your script is actually being loaded onto the page.

How can I subtract two time values in Adobe Sign?

I need to get the difference in hours and minutes of two times. For example 8:30AM and 2:14PM. I need to be able to subtract these to hours in Adobe Sign Formula expression. The supported function seem to support date based fields and not necessarily work right with only time. Here is the support page:
Add calculated fields to a form
I tried using datePart(part, date) and parsing first the hour and then the minutes. The problem is that this works if the value is a PM time (3:00PM) but if the value is an AM time (7:30AM) the function returns 0 instead of 7 (in the hours case)
datePart("h", "08:00:PM") = 8
datePart("h", "07:30:AM") = 0
I found out how to do this:
When creating the fields, make sure they are of type text and that the properties are as shown in the following image:
Notice the date format is a custom and with format h:nn tt, meaning that it only accepts values like 12:25 PM and 1:35 AM (no lower case am or pm). So suppose you have two fields like these: time_test1 and a time_test2, the third field instead will contain the number of hours will have this properties setup:
So, clicking on the F(x) in order to expand the formula expression, you need to put the following formula: dateDiff("h", [time_test2], [time_test1])
Also, click on Check syntax to make sure that it’s valid
Here is how it would look when I sign:
I hope this can be useful to others.

Getting %FD in resulting string when using Date.toLocaleString in javascript

I was looking for a way to get the month name and day-of-week name from a Date object in javascript and came across this post: Get month name from Date where David Storey pointed to the date.toLocaleString() method. At first it looked like this was working great. I could do something like this to get the month name without having to define an array:
var someDate = new Date(2018, 8, 28);
var locale = "en-us"
var month = someDate.toLocaleString(locale, { month: "short" }).toLowerCase();
that would give me a result of aug in the month variable. But I noticed a problem with this. Ultimately I was dropping the month and day-of-week values into hidden form fields that would get posted with the form. When the form posted I wasn't getting the expected values. For example, looking at the posted value for the month field, instead of aug I was getting %FDaug, and the same was true for the day-of-week value. But if I checked the values in the debugger prior to posting, they looked correct. I don't know where the %FD was coming from but it means I can't use this method unless there is some way to trim that off first. I looked at documentation for the function here but didn't see anything there related to this issue.
For now I'm back to using an array of month and day names and doing a lookup to get those. Anyone know why that extra ascii character is introduced when using Date.toLocaleString?
Here are screenshots from the browser debugger, I've tried this in IE, Edge, and Chrome. Chrome appears to be ok, but IE and Edge have this behavior. I haven't tried Firefox
the values before posting...
the posted values...

JavaScript mail function - strip datestamp portion from string

I am working with Google apps script and have a working function that sends emails based on a custom menu. The function that sends them is using a getRowsData() function to gather all the columns as objects, concatenating those objects into strings, and sending them via the htmlBody parameter, like this:
MailApp.sendEmail({
to: recipient,
subject: emailSubject,
htmlBody: message,
});
The message variable holds text and concatenated variables, with HTML formatting applied to construct the email. There is a portion that creates an Ordered list based on some other variables, and one of them is the date, which is pulled from a column within the sheet:
var message = "Other stuff here....Then"+
"<ol>"+
"<li>"+desc+"</li>"+
"<li>"+actions+"</li>"+
"<li>"+datesPer+"</li>"+
"<li><strong>Additional notes:</strong>"+actionNotes+"</li>"+
"</ol>"+
"More stuff below....";
When the email is sent, the datesPer variable prints out a bunch of other characters after the actual date. I'm assuming it's because the form attached to the sheet is inserting a datestamp:
3. Date: Tue Nov 08 2016 00:00:00 GMT-0800 (PST)
The 00:00:00 GMT-0800 (PST) is what I would like to change, with the zeroes using a regular time format (8:00 AM for example) and everything after that deleted. The array storing the dates has them formatted the way that they are coming out in the email, so this is a case of me telling the code exactly what I want, without telling it exactly what I want. I saw a Utilities.formatDate() class in the documentation, but it doesn't appear that I can call this into the script editor in sheets (Or I'm going about it wrong.)
If I missed a setting in the form, and this is just a checkbox somewhere, please educate me :) If it's something I can fix with code, I'd love some suggestions.
Thanks!
You can use javascript Date object functions to get the time from your date string.
You can find more details about it here: http://www.w3schools.com/js/js_date_methods.asp
Your script should have a line like this:
datesPer = "Date:" + TimeArray[0]
You can modify the date by creating a new Date object using the value from the array like so
var dateFromSheet = new Date (TimeArray[0])
var day = dateFromSheet.getDay() //(Returns 0- Sunday, 6 - Saturday)
var time = dateFromSheet.getHour() + ":" + dateFormSheet.getMinutes()
The values that you get from the above can be formatted to your liking and pass it to datesPer variable.

Sharepoint Action List - Due date wrongly converted probqbly due to timezone

I have deployed an action lis with a kind of dashboard. Through this, I would like to highlight the overdue action by adding the status on a dedicated column via javascript. My full script works well except the line where I want to get the Due Date.
For trying to identify the issue, I have simply copied the Due Date in an other column (check column), and the result is surprising!!!
Examples:
Due Date as displawed in the pop-up > Check column interpreted by the JS code
-30/07/2016>27/07/2016 22:00:00
-16/08/2016>17/08/2016 22:00:00
-01/08/2016>03:08:2016 22:00:00
Find hereafter an extract of the code:
var Status = oListItem.get_item('Status');
duedate = oListItem.get_item('DueDate');
oListItem.set_item('Check', oListItem.get_item('DueDate'));
It seems that the issue is related to the orignial Due Date column, qnd linked to a wrong conversion.
Does someone have an idea how to correct this issue?
I have found a solution or work around:
Create a calculated field which recreate the date from the Due Date field
In the JS, get the string from the calculated field, and then split it, et recreate the date with the JS code
Have fun!

Categories

Resources