Advertisement
RemcoE33

Country time zones

Mar 2nd, 2023
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Get country timezones with the current datetime
  3. *
  4. * @return {array} datetime, timezone, country, flag, continents
  5. * @customfunction
  6. */
  7. function COUNTRYTIMEZONES(){
  8.   const url = "https://restcountries.com/v3.1/all"
  9.   const resp = UrlFetchApp.fetch(url)
  10.   const headers = ["DateTime", "Timezone", "Country", "Flag", "Continents"]
  11.   const results = [];
  12.   const now = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd HH:MM:ss")
  13.  
  14.   JSON.parse(resp.getContentText())
  15.     .forEach(c => {
  16.       const name = c.name.official
  17.       const flag = c.flag
  18.       const continents = c.continents.join(", ")
  19.  
  20.       c.timezones.forEach(tz => {
  21.         const date = new Date(now + " " + tz)
  22.         results.push([date, tz, name, flag, continents])
  23.       })
  24.     })
  25.  
  26.   const sorted = results.sort((a, b) => {
  27.     if (a[1] < b[1]) return -1
  28.     if (a[1] > b[1]) return 1
  29.     return 0
  30.   })
  31.  
  32.   sorted.unshift(headers)
  33.  
  34.   return sorted
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement