• Sidereal.html
  • Free Sidereal Clock Javascript by Indigotide.
  • Design Intent: Windows Desktop Application
  • Features: UTC to GMST Conversion
  • To use it, all you need to do is set your local longitude.
  • Learn more about Sidereal Time
SIDEREAL CLOCK JAVASCRIPT
Local Time
UTC Time (GMT) GMST Angle GMST
Local Longitude
Year Day LST Angle
Local Sidereal Time
CODE - sidereal.js
<!--
// Begin Script "sidereal.js"

/* SIDEREAL CLOCK
Local Sidereal Clock for Windows Desktop By James Melatis
webmaster@indigotide.com
Click on set-up button to get a prompt window
Set longitude to decimalized local longitude to compute offset for Local Sidereal Time
Setting will round automatically to 7 decimal places
7 decimal places puts you within 1/2" at the equator (0.0004 arc seconds)
and even less closer to the poles
longitude = 0 = Greenwich Mean Sidereal Time (GMST)
longitude negative = West longitude offset
longitude positive = East longitude offset
EXAMPLE: West Longitude 117 31' 51.71988" = -117.5310333
*/

// execute this when the form loads

function loadUserSetting()
{ DefaultLongitude = -117.5310333; // Put YOUR default local longitude here...

clock.longitude.value = DefaultLongitude;

document.getElementById( "longitude" ).readOnly = true; // no typing in displays allowed
document.getElementById( "degrees" ).readOnly = true;
document.getElementById( "minutes" ).readOnly = true;
document.getElementById( "seconds" ).readOnly = true;
document.getElementById( "meridian" ).readOnly = true;
document.getElementById( "degrees2" ).readOnly = true;
document.getElementById( "meridian2" ).readOnly = true;
document.getElementById( "date" ).readOnly = true;
document.getElementById( "utc" ).readOnly = true;
document.getElementById( "gmst" ).readOnly = true;
document.getElementById( "day" ).readOnly = true;
document.getElementById( "angle" ).readOnly = true;
document.getElementById( "lst" ).readOnly = true;

var InputValue = parseFloat( clock.longitude.value );
UpdateLongitude( InputValue ); // update all longitude displays

UpdateClock(); // Have longitude so bail and start clock update routine
}

function UpdateClock() //loop to keep time displays current
{
var long = parseFloat (clock.longitude.value); // get longitude variable from current form INPUT text value
// and convert to floating point number

var now = new Date(); // get current date & time from computer clock

var date = now.toLocaleString(); // format date as local full date and 12 hour clock
var utc = now.toUTCString(); // format utc as UTC date & time

var beg = new Date( now.getUTCFullYear() - 1, 11, 31 ); // get last day of previous year in milliseconds
var day = Math.floor( ( now - beg ) / 86400000 ); // compute integer day of year (86400000 ms/day)

var mst = getGMST( now ); // get adjusted GMST in degrees for current system time
var mstAngle = mst; // save for GMST Angle display

// compute integer GMST hour angle deg min sec
var gmstdeg = Math.floor( mstAngle ); // get integer GMST hour angle degrees right ascension of vernal equinox

mstAngle = mstAngle - gmstdeg; // get integer GMST hour angle minutes right ascension of vernal equinox
mstAngle = mstAngle * 60;
var gmstmin = Math.floor( mstAngle );

mstAngle = mstAngle - gmstmin; // get integer GMST hour angle seconds right ascension of vernal equinox
mstAngle = mstAngle * 60;
var gmstsec = Math.floor( mstAngle );

var lst = mst + long; // now we know GMST so just add local longitude offset

if( lst > 0.0 ) // circle goes round and round, adjust LST if < 0 or > 360 degrees
{
while( lst > 360.0 )
lst -= 360.0;
}
else
{
while( lst < 0.0 )
lst += 360.0;
}

var ras = lst; // save LST degrees right ascension for hour angle display

lst = lst / 15.0; // change LST from degrees to time units (15 deg/hour)
mst = mst / 15.0; // change MST from degrees to time units (15 deg/hour)

// compute integer LST hour angle deg min sec
var deg = Math.floor( ras ); // get integer hour angle degrees right ascension of vernal equinox

ras = ras - deg; // get integer hour angle minutes right ascension of vernal equinox
ras = ras * 60;
var min = Math.floor( ras );

ras = ras - min; // get integer hour angle seconds right ascension of vernal equinox
ras = ras * 60;
var sec = Math.floor( ras );

// compute local sidereal time hour minute second
hour = Math.floor( lst ); // get integer LST hour

lst = lst - hour; // get integer LST minute
lst = lst * 60;
minute = Math.floor( lst );

lst = lst - minute; //get integer LST second
lst = lst * 60;
second = Math.floor( lst );
// compute GMST time hours minutes seconds
hours = Math.floor( mst ); // get integer MST hours

mst = mst - hours; // get integer MST minutes
mst = mst * 60;
minutes = Math.floor( mst );

mst = mst - minutes; //get integer MST seconds
mst = mst * 60;
seconds = Math.floor( mst );

document.clock.date.value = " " + date; // update "clock" form displays
document.clock.utc.value = " " + utc;
document.clock.gmstangle.value = " " + addZero( gmstdeg ) + " " + addZero( gmstmin ) + "\' " + addZero( gmstsec ) + "\"";
document.clock.gmst.value = " " + addZero( hours ) + " : " + addZero( minutes ) + " : " + addZero( seconds );
document.clock.day.value = " " + day ;
document.clock.angle.value = " " + addZero( deg ) + " " + addZero( min ) + "\' " + addZero( sec ) + "\"";
document.clock.lst.value = " " + addZero( hour ) + " : " + addZero( minute ) + " : " + addZero( second );

newtime = window.setTimeout("UpdateClock();", 1000); // update all clock displays once per second
}

function addZero( n ) // adds leading zero if 1 digit number
{
if( n < 10 )
{
return "0" + n;
}
else
return n;
}

// Function getGMST computes Mean Sidereal Time (J2000)
// Input: Current Date
// Returns: Adjusted Greenwich Mean Sidereal Time (GMST) in degrees

function getGMST( now )
{
var year = now.getUTCFullYear(); // get UTC from computer clock date & time (var now)
var month = now.getUTCMonth() + 1;
var day = now.getUTCDate();
var hour = now.getUTCHours();
var minute = now.getUTCMinutes();
var second = now.getUTCSeconds();

if( month == 1 || month == 2 )
{
year = year - 1;
month = month + 12;
}

var lc = Math.floor( year/100 ); //integer # days / leap century
var ly = 2 - lc + Math.floor( lc/4 ); //integer # days / leap year
var y = Math.floor(365.25 * year); //integer # days / year
var m = Math.floor(30.6001 * (month + 1)); //integer # days / month

// now get julian days since J2000.0
var jd = ly + y + m - 730550.5 + day + (hour + minute/60.0 + second/3600.0)/24.0;

// julian centuries since J2000.0
var jc = jd/36525.0;

// Greenwich Mean Sidereal Time (GMST) in degrees
var GMST = 280.46061837 + 360.98564736629*jd + 0.000387933*jc*jc - jc*jc*jc/38710000;

if( GMST > 0.0 ) // circle goes round and round, adjust if < 0 or > 360 degrees
{
while( GMST > 360.0 )
GMST -= 360.0;
}
else
{
while( GMST < 0.0 )
GMST += 360.0;
}

return GMST; // in degrees
}

function newLongitude()
{
var Prompt = "ENTER Complete Local Longitude in +/- DEGREES";
var Example = "Enter -117.5310333 for West Longitude 117 31' 51.71988\"";
var Default = DefaultLongitude;
var RangeMin = -180;
var RangeMax = 180;
var Decimals = 7; // 7 decimal places is 0.0000001 = 0.0004 arc seconds longitude is < 1/2" distance as the ant crawls

var InputValue = GetNumber( Prompt,Example,Default,RangeMin,RangeMax,Decimals ); // prompt for number value

if ( InputValue === false )
{
return;
}
else
{
UpdateLongitude(InputValue); // update longitude displays
}
}

function GetNumber( Prompt,Example,Default,RangeMin,RangeMax,Decimals )
{
var InputValue = prompt(Prompt + ": RANGE = ( " + RangeMin + " to " + RangeMax + " )\nEXAMPLE: " + Example , Default );

if ( InputValue == null || InputValue == "" )
{
return false; // prompt entry canceled or field returned blank so just ignore it
}
else if ( isNaN( InputValue ) == true )
{
alert("\"" +InputValue + "\" IS NOT A NUMBER: Please Input a Number...\nRANGE = ( " + RangeMin + " to " + RangeMax + " )");
return false; // prompt entry was not a number so alert user and ignore it
}
else if ( InputValue < RangeMin || InputValue > RangeMax )
{
alert("INPUT OUT OF RANGE: ( " + InputValue + " ) \nRANGE = ( " + RangeMin + " to " + RangeMax + " )");
return false; // prompt entry was a number but out of range so alert user and ignore it
}
else if ( Decimals == 0 )
{
InputValue = Math.floor(InputValue); // no decimals returned
return InputValue; // return it!
}
else
var InRange = new Number( InputValue ); // prompt entry number within range so create number object
InputValue = InRange.toFixed( Decimals ); // use number object to round to requested number of decimals
return InputValue; // return it!
}

function UpdateLongitude(InputValue)
{
var longitude = parseFloat(InputValue); // get decimal longitude from form input field

if ( longitude < 0.0 ) // Display Meridian value
{
document.clock.meridian.value = "West"; // negative = West
}
else if ( longitude > 0.0 )
{
document.clock.meridian.value = "East"; // positive = East
}
else
{
document.clock.meridian.value = "Prime"; // not E or W so must be Zero "Prime"
}

longitude = Math.abs(longitude); // throw away negative sign
var degrees = Math.floor( longitude ); // save integer degrees without sign

longitude = longitude - degrees; // get longitude minutes
longitude = longitude * 60;
var DecMinutes = longitude; // save decimal minutes
var minutes = Math.floor( longitude ); // save integer minutes

longitude = longitude - minutes; // get decimal longitude seconds
longitude = longitude * 60;
var seconds = longitude

var setDecimal = new Number( InputValue ); // restore longitude to number object
InputValue = setDecimal.toFixed(7); // use number object to round to 7 decimals

var setDecimal = new Number( DecMinutes ); // save minutes with decimals as object
var DecMinutes = setDecimal.toFixed(6); // use number object to round to 6 decimals

var setDecimal = new Number( seconds ); // save seconds with decimals as object
var seconds = setDecimal.toFixed(5); // use number object to round seconds to 5 decimals

document.clock.longitude.value = InputValue + ""; // update all longitude displays

document.clock.degrees.value = degrees + "";
document.clock.minutes.value = addZero( minutes ) + "\'";
document.clock.seconds.value = addZero( seconds ) + "\"";

document.clock.degrees2.value = document.clock.degrees.value
document.clock.dminutes.value = addZero( DecMinutes ) + "m";
document.clock.meridian2.value = document.clock.meridian.value

}

function ShowAbout()
{
alert(" SIDEREAL CLOCK Javascript\n A Virtual Sidereal Clock for Windows Desktop.\n\nTo find your local \"STAR\" time, click on the set-up button set your local longitude value. \n\nEnter your longitude as decimal +/- DEGREES: ( -117.5310333 )\nEnter a negative number for WEST longitude and a positive number for EAST.\n\nACCURACY: Decimal Degrees rounded down to 7 decimal places.\n0.0000001 = 0.0004 ArcSeconds, which is less than 1/2 inch at the equator. \n\n Please click on the clock displays for more information...\n\nCopyright November 16, 2008 - James Melatis - webmaster@indigotide.com ")
}

function ShowLST()
{
alert(" SIDEREAL CLOCK Javascript\nLOCAL SIDEREAL TIME (LST) is the Right Ascension (RA) of the sky objects\ncurrently on your North/South Line. ( WHERE YOU ARE )\n\nIn other words, LOCAL STAR TIME.\n\nCopyright November 16, 2008 - James Melatis - webmaster@indigotide.com ")
}

function ShowGMST()
{
alert(" SIDEREAL CLOCK Javascript\nGMST is Greenwich Mean Sidereal Time, or the current \"STAR\" time at longitude zero. \n\nCopyright November 16, 2008 - James Melatis - webmaster@indigotide.com ")
}

function ShowGmstAngle()
{
alert(" SIDEREAL CLOCK Javascript\nGMST ANGLE is the same as Greenwich Mean Sidereal Time (GMST),\nexcept it is shown in DEGREES rather than time units. \n\nIt is the HOUR ANGLE of the sky objects currently on the North/South Line at longitude zero. \n\nOnce you know this in DEGREES, you can just add your local longitude DEGREES \nto find your local Hour Angle, then divide by 15 to get Local Sidereal Time.\n\nCopyright November 16, 2008 - James Melatis - webmaster@indigotide.com ")
}

function ShowAngle()
{
alert(" SIDEREAL CLOCK Javascript\nLST ANGLE is the same as Local Sidereal Time, except it is shown in DEGREES rather than time units. \n\nIt is the HOUR ANGLE of the sky objects currently on your North/South Line.\n\nCopyright November 16, 2008 - James Melatis - webmaster@indigotide.com ")
}

function ShowUTC()
{
alert(" SIDEREAL CLOCK Javascript\nUTC TIME is Universal Coordinated Time and is the same time everywhere.\nIt is also the same as what used to be called Greenwich Mean Time (GMT). \n\nUTC is the current time and date at longitude zero.\n\nWith local time, \"WHEN\" something will happen depends on \"WHERE\" you are.\nInstead, UTC provides a global frame of reference when talking about \"WHEN\" \nan event will take place.\n\nCopyright November 16, 2008 - James Melatis - webmaster@indigotide.com ")
}

function ShowLocal()
{
alert(" SIDEREAL CLOCK Javascript\nLOCAL TIME is just the 12-hour time and date where YOU are. \n\nCopyright November 16, 2008 - James Melatis - webmaster@indigotide.com ")
}

function ShowDay()
{
alert(" SIDEREAL CLOCK Javascript\nYEAR DAY is just the current day of the year where YOU are. \n\nCopyright November 16, 2008 - James Melatis - webmaster@indigotide.com ")
}

// End Script "sidereal.js"
// unhide -->
/CODE



HTML - sidereal.html
<!DOCTYPE HTML>
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META http-equiv="Content-Style-Type" content="text/css">
<META http-equiv="Description" content="Free Sidereal Clock Javascript">
<TITLE>Free Sidereal Clock Javascript</TITLE>
<STYLE type="text/css">
<!--
INPUT{
font-family : Arial;
color : #ff0000;
background-color : #000000;
border-color : #000000;
font-weight : bolder;
}
BUTTON{
font-family : Arial;
color : #808080;
background-color : #000000;
border-style : solid;
border-color : #808080;
font-weight : bolder;
}
TABLE{
font-family : Arial;
color : #A0A0A0;
background-color : #000000;
font-size : xx-small;
font-weight : bolder;
}
BODY{
background-color : #000000;
}-->
</STYLE>
<SCRIPT language="JavaScript" src="sidereal.js"></SCRIPT>
</HEAD>
<BODY onload="loadUserSetting()" bgcolor="#000000">
<CENTER>
<FORM name="clock">
<TABLE border="0" width="200" height="100" cellspacing="5" style="border-size : 4px;border-color : black;border-style : solid;">

<TR>
<TD colspan="2" align="left">
<CENTER>
<TABLE border="0" cellspacing="5" style="font-size : small;">
<TBODY>
<TR>
<TD align="center">SIDEREAL CLOCK JAVASCRIPT</TD>
</TR>
<TR>
<TD align="center"><FONT size="-1">Local Time</FONT></TD>
</TR>
<TR>
<TD align="center"><INPUT id="date" value="Requires JavaScript" size="70" style="font-size : medium;text-align : center;" type="text" onclick="ShowLocal()"></TD>
</TR>
</TBODY>
</TABLE>
</CENTER>
</TD>
</TR>
<TR>
<TD colspan="2" align="left">
<TABLE border="0" cellspacing="5" style="font-size : small;">
<TBODY>
<TR>
<TD align="center"><FONT size="-1">UTC Time (GMT)</FONT></TD>
<TD align="center"><FONT size="-1">GMST Angle</FONT></TD>
<TD align="center"><FONT size="-1">GMST</FONT></TD>
</TR>
<TR>
<TD align="center"><INPUT id="utc" value="0" size="29" style="font-size : medium;" type="text" onclick="ShowUTC()"></TD>
<TD align="center"><INPUT id="gmstangle" value="0" size="8" style="font-size : medium;" type="text" onclick="ShowGmstAngle()"></TD>
<TD align="center"><INPUT id="gmst" value="0" size="8" style="font-size : medium;" type="text" onclick="ShowGMST()"></TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD align="left" valign="middle">
<TABLE border="0" style="font-size : small;">
<TBODY>
<TR>
<TD align="center" colspan="4">Local Longitude</TD>
</TR>
<TR>
<TD colspan="4">
<TABLE border="0" cellspacing="2" style="font-size : small;">
<TBODY>
<TR>
<TD><INPUT id="degrees" value="0" size="1" style="border-style : none;" type="text"></TD>
<TD align="center"><INPUT id="minutes" value="0" size="1" style="border-style : none;" type="text"></TD>
<TD><INPUT id="seconds" value="0" size="5" style="border-style : none;" type="text"></TD>
<TD><INPUT id="meridian" value="West" size="2" style="border-style : none;" type="text"></TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD colspan="4">
<TABLE border="0" cellspacing="2" style="font-size : small;">
<TBODY>
<TR>
<TD><INPUT id="degrees2" value="0" size="1" style="border-style : none;" type="text"></TD>
<TD align="center"><INPUT id="dminutes" value="0" size="7" style="border-style : none;" type="text"></TD>
<TD><INPUT id="meridian2" value="West" size="2" style="border-style : none;" type="text"></TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD colspan="4" align="center">
<TABLE border="0" cellspacing="2" style="font-size : small;">
<COL span="1">
<COL span="1" width="50">
<TBODY>
<TR>
<TD align="left"><INPUT class="userData" id="longitude" value="0" size="9" type="text"></TD>
<TD align="center">
<BUTTON type="button" name="set-up" value="set-up" onclick="newLongitude()">Set-up</BUTTON>
</TD>
<TD align="right">
<BUTTON type="button" name="about" value="about" onclick="ShowAbout()">About</BUTTON>
</TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
</TBODY>
</TABLE>
</TD>
<TD align="right" valign="bottom">
<TABLE border="0" cellspacing="5" style="font-size : small;">
<TBODY>
<TR>
<TD colspan="2">
<TABLE border="0" cellspacing="5" style="font-size : small;">
<TBODY>
<TR>
<TD align="center"><FONT size="-1">Year Day</FONT></TD>
<TD align="center"><FONT size="-1">LST Angle</FONT></TD>
</TR>
<TR>
<TD align="center"><INPUT id="day" value="0" size="6" style="font-size : medium;" type="text" onclick="ShowDay()"></TD>
<TD align="center"><INPUT id="angle" value="0" size="8" style="font-size : medium;" type="text" onclick="ShowAngle()"></TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD colspan="2" align="center">
<TABLE border="0" cellspacing="5" style="font-size : small;">
<TBODY>
<TR>
<TD align="center">Local Sidereal Time</TD>
</TR>
<TR>
<TD align="center" valign="top"><INPUT id="lst" value="0" size="8" style="font-size : x-large;" type="text" onclick="ShowLST()"></TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
</TABLE>
</FORM>
</CENTER>
</BODY>
</HTML>

/HTML
License: This javascript is FREEWARE, use it at your own risk. If you like it, please don't hesitate to share it with your friends or to modify it to suit your needs. All I ask is that it be distributed FREE of cost to help others to learn Javascript. If you want, you can pay me back by sending your help or comments about the script.

In no event will Web Designs By IndigoTide be liable to any party for any direct, indirect, special or other consequential damages for any use of this free software, including, without limitation, any lost profits, business interruption, loss of programs or other data, loss of images or web pages, loss of printed or electronic media, or otherwise, even if we are expressly advised of the possibility of such damages. This program is provided "As-Is" without warranty of any kind, either express or implied, including, but not limited to, the implied warranties of merchantability, fitness for a particular purpose, or non-infringement. Any use of this software indicates your full acceptance of all terms and conditions of this software license.

Animations and Logos!