I came across this useful javascript and node.js function the other day. You can find the details of the original on github here. Here is the license – please respect the terms of the author if you reuse. I am also using moment.js in the webapp version.

What is SunCalc

Here is what the author says.

SunCalc is a tiny BSD-licensed JavaScript library for calculating sun position, sunlight phases (times for sunrise, sunset, dusk, etc.), moon position and lunar phase for the given location and time, created by Vladimir Agafonkin (@mourner) as a part of the SunCalc.net project.

Most calculations are based on the formulas given in the excellent Astronomy Answers articles about position of the sun and the planets. You can read about different twilight phases calculated by SunCalc in the Twilight article on Wikipedia.”

The GAS version

The Google Apps Script version didn’t need much tweaking, and I provide both a library (to include in your projects) and a webapp version (you can use as a rest server.. JSON and JSONP are supported)

The library

You can include this in your project by referencing this library MrUiju_lhzV9x8s701Cw5QSz3TLx7pV4j

Here’s an example of how to use.

function testSunCalc() {

  // get today's sunlight times for empire state building
  var sun = new SunCalc.SunCalc();
  var times = sun.getTimes(new Date(), 40.7484, 73.9857);

The result

    "solarNoon": "2014-03-31T07:09:28.774Z",
    "nadir": "2014-03-30T19:09:28.774Z",
    "sunrise": "2014-03-31T00:51:02.884Z",
    "sunset": "2014-03-31T13:27:54.664Z",
    "sunriseEnd": "2014-03-31T00:53:51.667Z",
    "sunsetStart": "2014-03-31T13:25:05.881Z",
    "dawn": "2014-03-31T00:23:27.057Z",
    "dusk": "2014-03-31T13:55:30.491Z",
    "nauticalDawn": "2014-03-30T23:50:52.414Z",
    "nauticalDusk": "2014-03-31T14:28:05.134Z",
    "nightEnd": "2014-03-30T23:17:19.113Z",
    "night": "2014-03-31T15:01:38.435Z",
    "goldenHourEnd": "2014-03-31T01:27:10.810Z",
    "goldenHour": "2014-03-31T12:51:46.738Z"

The Web app.

The URL for the webapp is here.

Usage is pretty straightforward. The parameters are

 parameter  what it is  default  format  example
 date  the date to calculate times for.  Today (UTC)  UTC format date including timezone &date=”2013-03-31T17:47:37.150-10:00″
 lat  the latitude of the place it applies to  51.477812  number  &lat=40.1
 lon  the longtitude of the place it applies to  -0.000725  number  &lon=15.7
 callback  callback function name will generate JSONP response  null  string  &callback=somefunction


Get the sun activity times for Sao Paulo, Brasil on Christmas day this year.



    "error": null,
    "params": {
        "callback": null,
        "lat": "23.55",
        "lon": "46.63",
        "date": "2014-12-25T15:00:00+00:00"
    "result": {
        "solarNoon": "2014-12-25T08:54:38.799Z",
        "nadir": "2014-12-24T20:54:38.799Z",
        "sunrise": "2014-12-25T03:34:07.338Z",
        "sunset": "2014-12-25T14:15:10.259Z",
        "sunriseEnd": "2014-12-25T03:36:40.946Z",
        "sunsetStart": "2014-12-25T14:12:36.652Z",
        "dawn": "2014-12-25T03:09:24.539Z",
        "dusk": "2014-12-25T14:39:53.059Z",
        "nauticalDawn": "2014-12-25T02:41:13.704Z",
        "nauticalDusk": "2014-12-25T15:08:03.894Z",
        "nightEnd": "2014-12-25T02:13:28.365Z",
        "night": "2014-12-25T15:35:49.233Z",
        "goldenHourEnd": "2014-12-25T04:07:34.354Z",
        "goldenHour": "2014-12-25T13:41:43.243Z"

Note on times

All times are expressed in UTC – in other words the greenwich mean time point of view. You can convert these to your locale using .toLocaleString()

You can specify a timezone offset with the &date parameter, but the returned times will always be UTC. Note that result.params.data contains your date parameter converted to UTC time.

Web app code

sunCalcRest created by GitJsonApi
0 forks.
3 stars.
0 open issues.

Recent commits:

Code for SunCalc

You can include this in your project by referencing this library MrUiju_lhzV9x8s701Cw5QSz3TLx7pV4j

SunCalc created by GitJsonApi
0 forks.
2 stars.
0 open issues.

Recent commits:

I also have a Google Apps Version version of moments.js, which you can use from Ma8KMulU4F1uDjuQWo4W1oSz3TLx7pV4j

For more like this see Google Apps Scripts Snippets
For help and more information join our forum, follow the blog, follow me on Twitter