Trying to subtract 5 days from a defined date - Google App Script - google-apps-script

I'm trying to write a script which is supposed to send out an email and create two calender entries when submitting a form. To be honest, this is my first script and I am very happy that the email is send out and the calender entries are working as well. The thing which gives me a headache is to subtract 5 days (actually x days) from a defined date.
First I thought I could simply do something like
var liveday = e.values[2];
var newday = liveday-5;
well, this didn't work :-)
I tried more:
var newtime = new Date(liveday);
var yr = newtime.getYear();
var dt = newtime.getDay();
var mt = newtime.getMonth();
var dtnew = dtnew.setDate(mt, dt-5, yr);
But here I received 1418256000000 whereas liveday = 12/01/2014. Not sure why days were added, rather than subtracted.
I am quite confused here and the answer can't be that hard.
I just want to subtract 5 days from 12/01/2014 to receive 11/27/2014.
Thanks for having a look

the comment sends you to a rather complicated serie of codes... there is a far more simple way to get that, here is the code :
function test() {
Logger.log('today= '+new Date()+' and 5 days ago is '+subDaysFromDate(new Date(),5));
}
function subDaysFromDate(date,d){
// d = number of day ro substract and date = start date
var result = new Date(date.getTime()-d*(24*3600*1000));
return result
}
Logger result :
[13-11-18 23:39:50:364 CET] today= Mon Nov 18 2013 23:39:50 GMT+0100 (CET) and 5 days ago is Wed Nov 13 2013 23:39:50 GMT+0100 (CET)
if you want to get the date in the form dd/mm/yyyy use Utilities.formatDate(date, timeZone, 'dd/MM/yyyy), see doc here

Related

google app script how to get date in string date(spread sheet)

I want to compare the date that was created in the spreadsheet with a specific date.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var spreaddate = ss.getSheetByName("sheet123").getRange("B2").getValue();
//--> Thu Dec 12 00:00:00 GMT+09:00 2019
var comparedate = new date(yyyy-mm-dd)//somting like this
if(spreaddate > comparedate){
do something
}
The spreaddate is not recognized as a date.
Even if I try to convert it to a date, it's hard because it's written in letters, not numbers like Thu Dec.
What should I do?
I think the problem is that you're not correctly defining your comparedate. You need to use a capital 'D' new Date().

How to get google sheet's cell values as is using google script

I'm about to use google sheet as my database for my android app small project. I'm using Google Script to handle the request from my app.
In my google sheet, I store;
A2:A = date as dd/mm/yyyy e.g 21/12/2019 but
the display format is dd-MMM e.g 21-Dec
C2:D = time as hh:mm:ss e.g 21:00:00 but
the display format is hh:mm e.g 21:00
Yes, I need a different format for the display and input.
My google sheet:
When I use google script to get a value of the cell, it seems that it is reformatted
the date looks like this: Sat Jan 01 2019 00:00:00 GMT+0700 (ICT)
the time the other hand, change in value a bit. 20:00:00 to 19:52:48
Is there any function to get cell real values as text without being reformatted?
The only thing that I can think of is instead of using getValues(), I can use getDisplayValues(). The values will not be reformatted, but it is not a solution for me, as it will take the display format.
Snippet of my code:
function updateData(e, sheet) {
var tgl = e.parameter.tgl;
var dtg = e.parameter.dtg;
var plg = e.parameter.plg;
var lbr = e.parameter.lbr;
var rangeHead = sheet.getRange("A2:A");
var valuesHead = rangeHead.getValues();
var rangeFirst = sheet.getRange("C2:D")
var valuesFirst = rangeFirst.getValues();
var rangeSecond = sheet.getRange("G2:G")
var valuesSecond = rangeSecond.getValues();
for (var i = 0; i < valuesHead.length; i++) {
if (valuesHead[i][0] === tgl) {
if(dtg!="null") { valuesFirst[i][0] = dtg; }
if(plg!="null") { valuesFirst[i][1] = plg; }
if(lbr!="null") { valuesSecond[i][0] = lbr; }
break;
}
}
rangeFirst.setValues(valuesFirst);
rangeSecond.setValues(valuesSecond);
}
The code won't work as I will comparing 21/12/2019 with Sat Jan 01 2019 00:00:00 GMT+0700 (GMT).
[UPDATE 1]:
Thank you P-Burke for the enlightenment. Now, I have an idea to solve the date problem. I know that the script pulls the date as date object, but I am unaware that it also saves as a date object. (hehe my bad) I don't realize it as there is no autocomplete when I call values[0][0]. of course, as it recognizes the object type at the run time.
So, my workaround will be; I will call getDate, getMonth+1, and getYear. After that, I will compare with my parameter freely.
Though, the time cell still a bit confusing for me. the time offset is 18 minutes 12 seconds. I don't think it's because of timezone different and my computer clock. the timezone different is too big and I 've made sure that the script, spreadsheet, and local timezone all the same. My computer clock is also only a minute less behind.
[UPDATE 2]:
Alright, enough with the confusion. It seemed that the script converts the time to Date object respect to my local timezone. I got this answer from another thread. So, actually, my local timezone changes many times and some of them have offset smaller than hours unit (one of the timezones used in my area is UTC +7:07:12h). The only source documenting those changes I could find is from https://www.timeanddate.com/time/zone/indonesia/jakarta. Finally, I gave up. For my goodness sake, I will just use getDisplayValue and ignore the seconds. Unless you guys have any other workaround, I will be so grateful.
Thank you once again to the community.
Firstly, and I don't know if this is related to your issue, but the spreadsheet and the script each have their own timezone setting:
Spreadsheet: File >> Spreadsheet Settings >> Time Zone.
Script: File >> Project Properties >> Time Zone.
And if these are different that can lead to confusion. One answer, if all your users are in the same timezone, is to set them to the same. Alternatively these can be determined from within your script as described here, and logic included to handle any differences. I don't understand the few minutes time difference, perhaps your PC clock is inaccurate?
The other point, which I think is more relevant to your question is that you effectively have multiple date/time formats in play. The picture below shows that in the spreadsheet times are edited in one format (02/01/2019 09:00:00), but displayed in whatever format is defined for the cell using the format menu. Yet when the cell values are pulled into a script using getValues() and displayed they appear as follows: Values: [[Thu Jan 31 09:00:00 GMT+00:00 2019, Wed Jan 02 09:00:00 GMT+00:00 2019]].
Yet in the code below, values[0][0] and values[0][1] are actually JavaScript Date() objects and can be compared in the usual way, alternatively they can be reformatted into whatever string format you require as illustrated in the code below:
function myFunction() {
var ss = SpreadsheetApp.getActive();
var ws = ss.getActiveSheet();
var input_range = ws.getRange("A1:B1");
var values = [];
values = input_range.getValues(); // Returns a multi-dimensional array, hence [0][0] to access.
Logger.log("Values: %s", values);
// As Date() objects the usual methods are available.
Logger.log("Date().getMonth(): %s",values[0][0].getMonth());
Logger.log("Date().getYear(): %s",values[0][1].getYear());
// This formats the date as Greenwich Mean Time in the format
// year-month-dateThour-minute-second.
var formattedDate = Utilities.formatDate(values[0][0], "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
Logger.log(formattedDate);
formattedDate = Utilities.formatDate(values[0][1], "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
Logger.log(formattedDate);
}
Logger.log output:
[19-01-31 11:43:17:635 GMT] Values: [[Thu Jan 31 09:00:00 GMT+00:00 2019, Wed Jan 02 09:00:00 GMT+00:00 2019]]
[19-01-31 11:43:17:636 GMT] Date().getMonth(): 0.0
[19-01-31 11:43:17:637 GMT] Date().getYear(): 2019.0
[19-01-31 11:43:17:638 GMT] 2019-01-31T09:00:00Z
[19-01-31 11:43:17:638 GMT] 2019-01-02T09:00:00Z

Using dates in Google Sheets

Dates are giving a bit of trouble in Google Sheets. I'm basically trying to subtract dates but it's not working. However first, I'm trying to understand how Dates work. Here's an example of an odd behavior with dates.
function addDates(sheet)
{
var prevDateCurrYear = new Date();
Logger.log(prevDateCurrYear);
Logger.log(prevDateCurrYear.getMonth()+' '+prevDateCurrYear.getDay()+' '+prevDateCurrYear.getYear());
}
This is the Log
[15-02-03 18:15:21:631 EST] Tue Feb 03 18:15:21 GMT-05:00 2015
[15-02-03 18:15:21:632 EST] 1 2 2015
The getMonth and getDay doesn't seem to work. It should be pulling 2 and 3 but instead pulls 1 and 2. Why is this happening?
I'm using this documentation: https://developers.google.com/apps-script/reference/contacts/date-field
Actually getMonth and getDay are Java script methods. The way the getMonth and getDay works different in Javascript. You can refer to these pages for more information.
http://www.w3schools.com/jsref/jsref_getday.asp
http://www.w3schools.com/jsref/jsref_getmonth.asp
Hope that helps!
Couple things to consider.
1. Set the Time Zone in your script Project Properties. File, Project Properties.
2. Using Utilities.formatDate() can relieve a lot of headaches as you can standardize your dates to your needs.
3. getDay() and getDate() return "day of the week (i.e. 1-7)" and "the date of the month (i.e. 1-31)", respectively.
here is some more details about dates to clarify:
function addDates() {
var prevDateCurrYear = new Date();
var year = Utilities.formatDate(prevDateCurrYear, "America/Denver", "yyyy");
var date = Utilities.formatDate(prevDateCurrYear, "America/Denver", "d");
var month = Utilities.formatDate(prevDateCurrYear, "America/Denver", "M");
Logger.log("\nUnformatted prevDateCurrYear: "+prevDateCurrYear+
"\n\nYour original log:\nprevDateCurrYear.getMonth(): "+prevDateCurrYear.getMonth()+"\n"+
"prevDateCurrYear.getDay(): "+prevDateCurrYear.getDay()+"\n"+
"prevDateCurrYear.getYear(): "+prevDateCurrYear.getYear()+"\n"+
"\nUsing Utilites.formatDate():\nmonth: "+month+"\ndate: "+date+"\nyear: "+year+
"\nUtilities.formatDate(prevDateCurrYear, \"America/Denver\", \"M d yyyy\"): "
+Utilities.formatDate(prevDateCurrYear, "America/Denver", "M d yyyy"));
}
Log:
Unformatted prevDateCurrYear: Thu Feb 18 2016 11:29:42 GMT-0700 (MST)
Your original log:
prevDateCurrYear.getMonth(): 1
prevDateCurrYear.getDay(): 4
prevDateCurrYear.getYear(): 2016
Using 'Utilites.formatDate()`:
month: 2
date: 18
year: 2016
Utilities.formatDate(prevDateCurrYear, "America/Denver", "M d yyyy"): 2 18 2016

Getting day of the week not working when using setNumberFormat

I would like programably enter the date into a cell and format it to include the Day of the week (Mon, Tue, Wed,...). If I use the .setNumberFormat method (which I would prefer to do because it keeps the info as a date), the simpleDateFormat for Day of the week does not work. If I use Utilities.formatDate I can use 'EEE, MM/dd' and it will show up correctly, but I lose the date format.
function setformat(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var lab = ss.getSheetByName("test2");
// This is what I want to use but the day of the week 'EEE' doesn't work
var todaySNF = new Date();
lab.getRange("a1").setValue(todaySNF);
lab.getRange("a1").setNumberFormat('EEE, MM/dd'); // Should read Wed, 09/23 but reads EEE, 09/23 instead.
var cellA1asDate = new Date(lab.getRange("a1").getValue());
Logger.log(cellA1asDate);}
use this instead :
lab.getRange("a1").setNumberFormat('DDD, MM/dd');

Google Script- Convert Date to Epoch Time

I've been working on this for a long time and haven't quite found anything that fits what I need.
Right now, I have an array of dates- all of them look like this -
Sun Mar 31 2013 00:00:00 GMT+0700 (ICT).
I'm looking for a way to convert that into
1364688000
Only through Google Script.
How would you go about doing this?
=(A3-DATE(1970,1,1))*86400
With A3 being something like "2/20/2018"
The getTime() method applied to a date returns the number of milliseconds since the epoch reference in JavaScript, ie what you are looking for.
Logger.log(new Date(2013,3,31,0,0,0,0).getTime());
Your value is in seconds, so we can divide /1000
but the value you are showing is not correct, result has an offset of 31 days in GMT 0 ... How are you getting the value 1364688000 ?
test code (script properties in UTC (GMT 0 without daylight savings)
function timeInMsSinceOrigin(){
var val = new Date(2013,3,31,0,0,0,0).getTime()/1000;// in seconds
var offset = val-1364688000; //in seconds too
Logger.log('value = '+val+ ' offset : '+offset/3600+' hours ='+offset/(3600*24)+' days');
}
Logger result : value = 1367366400 offset : 744 hours =31 days