ahmed
ahmed

Reputation: 14666

How to calculate the sunrise and sunset in javascript?

i am using appcelerator titanium to develop an IOS app and i want to let my app send local notification to the users in sunrise and sunset

a good tool to solve this is Yahoo weather using YQL ; however, yahoo weather is for non-commercial use only!!

i am trying to find a javascript library to calculate the sunrise/set + a database of the world's countries and cities with the latitude and longtude

have anyone ever needed the same?

Any idea.

Upvotes: 3

Views: 7230

Answers (4)

Demian Brecht
Demian Brecht

Reputation: 21378

This algorithm looks pretty wicked to solve exactly what you're looking to do.

Upvotes: 7

TreyA
TreyA

Reputation: 3437

I wrote a Google Gadget (javascript) for sunrise and sunset calculation based on lat/lon located here.

It was written from algorithms in Duffett-Smith 'Practical Astronomy with your Calculator'.

Upvotes: 0

mattexx
mattexx

Reputation: 6616

Use NOAA's javascript routines for the solar position: here

They include a short list of cities with latitude and longitude. I think you can also get that from iOS, but here's another answer on getting it from city, state, etc.

Upvotes: 1

RobG
RobG

Reputation: 147523

With iOS (and likely other location enabled devices) you can get the users current location if they allow it. You can then use the local time and timezone offset to calculate the sunrise and sunset from their latitude and longitude.

It won't take into account local geographic effects on the horizon (e.g. if they are in a valley the sun will rise or set earlier than if they are on a hill) but it should be close enough. Use Google, I'm sure you'll find it - ah, I see Demian has done it for you.

Upvotes: -1

Related Questions