1
votes

Using google app script, I am accessing a google sheet of a rota with one sheet per year, with a row per day, with a column for the date.

To select the row of today's date, I would like to use something similar to getDay() to get the current days through the year (e.g. for 2020-02-11, the 42nd day of the year).

I've found the getDay() function in the docs but this is by month.

To avoid a function that calculates this (or a superfluous field in the source sheet, or scanning the rows for a date), is there a native function or simple way in google apps script to get the number of the current day of the year?

2

2 Answers

3
votes

You could try using the JS date format with Utilities.formatDate() to get the day of the year like so:

function dayOfTheYear() {
  var d = Utilities.formatDate(new Date(), 'UTC', 'D');
  Logger.log(d);
}
1
votes

In short, no; there's not a native function for getting the current day of the year.

The getDay() method you referenced is part of the ContactsApp, which means you would have to instantiate that, make any necessary calls to get a DateField object, and then call that method. That operation, although native to Apps Script, would likely take much longer than a simple JS function like any posted here.

Apps Script is basically just JavaScript with some additional objects that allow you to easily integrate with Google services. It has very few native "general" methods like what you're looking for, but most would be listed in the Utilities class.