'How to get week number in google-apps-script
How to get week number in google-apps-script?
For example, this week is Week37, How to get the 37 from google apps script?
Thank you very much..
Solution 1:[1]
Maybe there was no such functionality in 2015, but now just you can:
var data = Utilities.formatDate(new Date(), "GMT", "'Week'w");
// data = 'Week48' for example
You can use Utilities.formatDate(new Date(), "GMT", "u") which returns the number of the day in a week (1-Monday, 7-Sunday) and update week number depending on its value.
Solution 2:[2]
By default, Google Sheets start to count week numbers from Sunday. If u want to start count from Monday you can do something like that:
function getWeek(date) {
return Number(Utilities.formatDate(new Date(date), "Europe/Kiev", "u")) === 7 ?
Number(Utilities.formatDate(new Date(date), "Europe/Kiev", "w")) - 1 :
Number(Utilities.formatDate(new Date(date), "Europe/Kiev", "w"));
}
So the code will analyze if the day of the week equal to 7 (Sunday) it will returns (currentWeek - 1), else currentWeek.
And you have to specify timeZone param to your country time zone. This is the second param in Utilities.formatDate, in my case that was "Europe/Kiev", but you can find zone that you need here: https://developers.google.com/adwords/api/docs/appendix/codes-formats#timezone-ids
If you need some more information what params you can throw in Utilities.formatDate you can find it here: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | isherwood |
| Solution 2 | isherwood |
