Calculating the time within a day uses modulo arithmetic to find the remainder after calculating the day number.
The formula for calculating time within the day is shown here:
t = an instant in time measured in milliseconds relative to 01-January-1970 UTC.
msPerDay = 86400000
TimeWithinDay(t) = t modulo msPerDay
// Define some constants HoursPerDay = 24; MinutesPerHour = 60; SecondsPerMinute = 60; msPerSecond = 1000; // Derived values msPerMinute = msPerSecond * SecondsPerMinute; msPerHour = msPerMinute * MinutesPerHour; msPerDay = msPerHour * HoursPerDay; // Grab the time now in milliseconds myMilliseconds = Number(new Date()); document.write("Time within day ...: "); document.write(TimeWithinDay(myMilliseconds)); document.write("<BR>"); // Work out the millisecond time value function TimeWithinDay(aMillisecondTime) { return (aMillisecondTime % msPerDay); }
See also: | Broken down time, Day number, Time from year, Time range |
Prev | Home | Next |
Time value | Up | TimeClip() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |