I have a Google App Script that sends me an email whenever a certain event is triggered. This script has been running fine, but today, it's not sending the email with the correct subject anymore. I am based in New Zealand and today just so happens to be our daylight saving cutover. The timezone here is GMT+12. I believe the issue is that Google App Script isn't using daylight saving, even though it is configured to.
Here's the main part of the script that generates the email subject:
var date = Utilities.formatDate(new Date(), "GMT+12","dd-MM-yyyy' ----------- 'EEEE' ----------- 'hh:mm a' ----------- 'ss:SS' seconds ----------- 'Z' '");
The output of the email subject looks like this:
As you can see, the timezone is correct, but the time is not. The time should be 07:12 PM.
In the script/project, the timezone is set to the correct one. I'm not sure why the output is incorrect?
Nevermind, have solved this. Despite finding multiple answers on Google & StackOverFlow suggesting to use GMT+12, I ended up just using this:
var date = Utilities.formatDate(new Date(),"Pacific/Auckland","dd-MM-yyyy' ----------- 'EEEE' ----------- 'hh:mm a' ----------- 'ss:SS' seconds ----------- 'Z' '");
Related
I'm reading an entire Gmail emails (inbox/sent/trash...), and try to create folders in google drive according to dates.
So folders should be categorized like (...,2019,2020, 2021). Each folder contains all emails in this year in my Gmail.
I managed to create the folders, getting messages through using Threads, all these things works fine, but I couldn't extract the year of the message only.
As when I use Threads[0].getMessages()[0].getDate() what is returned is date and time of this message. which I can't extract the year only from it.
I tried to split the date, but this is not string.
I tried to make smth like Threads[0].getMessages()[0].getDate().getYear() it returned 120!!!
I tried to see if I can access it as an array, [index], it returned null
I tried to parse it to Date(), but I couldn't get use of it too.
Is there is anyway that allows me to extract only the year from this format.
This is part of the script where i'm trying to fetch the date I'm using.
function GetAttachmentsWithTime() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.setActiveSheet(sheet.getSheetByName('Sheet1'));
var start = 0;
var end = 10;
var filter = "has:attachment";
var threads = GmailApp.search(filter,start,end);
Logger.log(threads[0].getMessages()[0].getDate())
}
Are you using the V8 runtime?
If so, see this migration guide. It says:
In the V8 runtime, Date.prototype.getYear() returns the year minus
1900 instead as required by ECMAScript standards.
So emails from the year 2020 would return 120 (i.e. 2020 - 1900 = 120).
To fix this, use .getFullYear():
When migrating your script to V8, always use
Date.prototype.getFullYear(), which returns a four-digit year
regardless of the date.
I'm trying to set correct times for an Outlook event in the UK, but Outlook seems to set the incorrect time for BST (British Summer Time).
Here is a link, which creates an event for 19th June 2017 at 1254pm to 1257pm.
In my Outlook it opens an hour ahead as 1354pm. Is there a way to explicitly set the timezone in the link?
I've tried checking my settings and using a different Outlook account so I don't think it's an issue with my mail/calendar settings.
https://bay02.calendar.live.com/calendar/calendar.aspx?rru=addevent&dtstart=20170619T125400&dtend=20170619T125700&summary=Summary+of+the+event&location=Location+of+the+event&description=example+text.&allday=false&uid=
After researching quite I've realised that Outlook always expresses time in UTC when sending a link. Therefore you need to convert the date/time from BST to UTC. You can do this with PHP like this:
$date = new DateTime('2017-06-22T12:54', new
DateTimeZone('Europe/London')); /* <-- Time zone to be converted */
echo $date->format('YmdHis') . "\n";$date->setTimezone(new
DateTimeZone('UTC'));echo $date->format('YmdHis') . "\n"; /* <-- New time zone, UTC */
I am trying to format date stored in lead object via velocity email script token and it is not working, please anyone got same issue??
Here is what I did in Email Script token.
#set($myDate = $convert.parseDate(${lead.eventdate}, "dd-MM-yyyy"))
#set($formattedDate = $date.format("yyyy-MM-dd", $myDate))
${formattedDate}
When I see lead.eventdate it is stored like 23 Mar 2016.
trick was while parsing date, my Client is using French instance of marketo so when I see date in lead database was different then what it is actually stored.
finally slight change in parsing worked.
#set($myDate = $convert.parseDate(${lead.eventdate}, 'MMM dd, yyyy'))
#set($formattedDate = $date.format('dd/MM/yyyy', ${myDate}))
${formattedDate}
I'm writing a custom function to return financial quotes obtained from Yahoo Finance to a Google Sheets spreadsheet (the built-in GoogleFinance() won't cut it for various reasons), and I'm having problems to return a Date object containing the date and time of the quote. https://developers.google.com/apps-script/guides/sheets/functions says that "Times and dates in Sheets become Date objects in Apps Script. If the spreadsheet and the script use different time zones (a rare problem), the custom function will need to compensate", but it's not clear how.
First of all, the timezone of my browser is "asia/hong_kong", i.e. GMT+0800 (HKT). It turns out that the script somehow knows that, because when I format a Date object built with d = new Date(2016,5,24,0,0,0,0) with d.toString() I get "Wed Jun 24 2016 00:00:00 GMT+0800 (HKT)". This is weird, because SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone() actually returns "America/Los_Angeles", indicating that the server is on the West Coast of the US: so why does the method toString(), which is run on the server side, format the date according to the Hong Kong time? This sounds like a bug to me.
Anyway, the biggest problem I'm having is that when the custom function returns the date object to the spreadsheet, if I format the cell with Format -> Number -> Date time, what I see displayed is "23/06/2016 09:00:00", i.e. the date and time on the US West Coast when the Hong Kong date and time are the ones I specified through the parameters passed to the constructor (corresponding to Wed Jun 24 2016 00:00:00).
Then I tried to build the Date object with new Date(Date.UTC(2016,5,24,0,0,0,0)), but (predictably enough) now the spreadsheet displays "23/06/2016 17:00:00", which is the date and time on the West Coast when the UTC date and time are the ones I specified.
So, how do I get a Date object that, once returned to the spreadsheet, shows exactly the same date and time I used to build it? I tried to change the spreadsheet's timezone to UTC using ss.setSpreadsheetTimeZone("Etc/UTC"), but then unfortunately the script returns the error "You do not have permission to call setSpreadsheetTimeZone"...
This is the kludgy solution I managed to write (but I hope there is a better way):
// hours and minutes contain values obtained from the text quote; the date is in the string variable:
// date_str = "05/24/2016";
var tz = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(); // this returns the TZ of the server, "America/Los_Angeles"
var ums = Date.parse(date_str+" UTC");
var offset = Date.parse(Utilities.formatDate(new Date(ums), tz, "MM/dd/yyyy HH:mm:ss") + " UTC") - ums; // time in LA - UTC time in ms
var d = new Date(Date.parse(date_str));
d = new Date(Date.UTC(d.getFullYear(),d.getMonth(),d.getDate(),hours,minutes,d.getSeconds())-offset);
return d;
I need to:
Set the form to "Not accepting responses" every weekday at 7:16 am
Then set the form to “Accepting responses” every weekday at 5:30 am
Clear all responses from the form and the form spreadsheet on Friday at 2:00 pm
I have no experience with writing scripts. This is for a teacher sign-in sheet for a public high school.
Any help is greatly appreciated!
Unfortunately, StackOverflow isn't here to write your code for you, so you won't receive an answer that you can copy/paste and get to work. The community will instead assist you with any specific roadblocks that you encounter with your code (If they can).
However, you'll be able to achieve this with the Form Service, specifically, the '.setAcceptingResponses()' method. You'll also need the Spreadsheet service with the '.clear()' method to empty the sheet.
You'll find the time triggers are the easiest way to set when the form becomes active and inactive again, however, you've mention very specific times that this needs to go offline and online again (7:16 am and 5:30 am, but only on weekdays, and 2PM to clear the sheet on Fridays), but Google Apps script time triggers can't be set down to the minute (Note the triggers explanation about the time being randomized).
An alternative would be to write a function that checks the time, have that function run every minute, and if the time = 2PM on a Friday then clear the sheet etc.
have you checked out formLimiter by New Visions Cloud Lab?
http://cloudlab.newvisions.org/add-ons/formlimiter
The description is:
formLimiter automatically sets Google Forms to stop accepting responses after a maximum number of responses, at a specific date and time, or when a spreadsheet cell contains a specified value.
Great for time-bound assignments, event registrations with limited seats, or other first-come, first-served signup scenarios.
I was able to write a short script and set a trigger, I named my form "Play"
Here is the script:
function limitDays() {
var form = FormApp.getActiveForm();
var lastDay = 21;
var currDate = new Date();
var dayOfMonth = currDate.getDate();
if (dayOfMonth > lastDay) {
form.setAcceptingResponses(false);
} else {
form.setAcceptingResponses(true);
}
}