<%/* ============================================================================= DateTime.asp VERSIONINFO Version: __PUT_PHAROS_VERSION_HERE__ Copyright: Copyright © 2004 Pharos Systems Inc Description: Common functions related to date and time formatting and calculations Notes: This file is included by DateTimeClient.asp and many of the common functions are directly converted to client-side script. When making changes to this file note that the code may run in a browser as well. Warning: This software is property of Pharos Systems New Zealand Ltd. Unauthorized use or duplication of this software is strictly prohibited. Authorized users are subject to the following restrictions: * Neither the author, their corporation, nor Pharos Systems New Zealand Ltd. is responsible for any consequences of the use of this software. * The origin of this software must not be misrepresented either by explicit claim or by omission. * Altered versions of this software must be plainly marked as such. * This notice may not be removed or altered. ================================================================================ */%> <% var DayNames = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]; var ShortDayNames = [ "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" ]; var MonthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; var ShortMonthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]; // Arrays for RelativeDate var OrdinalMatchPattern = new Array(); OrdinalMatchPattern[ "day" ] = [ 0, 1, 2, 3, 4, 5, 6 ]; OrdinalMatchPattern[ "weekday" ] = [ 1, 2, 3, 4, 5 ]; OrdinalMatchPattern[ "weekend day" ] = [ 0, 6 ]; for ( DayIndex in DayNames ) { OrdinalMatchPattern[ DayNames[ DayIndex ] ] = [ parseInt( DayIndex, 10 ) ]; } var Ordinals = new Array(); Ordinals[ "first" ] = 1; Ordinals[ "second" ] = 2; Ordinals[ "third" ] = 3; Ordinals[ "fourth" ] = 4; Ordinals[ "last" ] = 1; function CompareDateOnly( First, Second ) { return ( ( First.getDate() == Second.getDate() ) && ( First.getMonth() == Second.getMonth() ) && ( First.getFullYear() == Second.getFullYear() ) ); } function CompareTimeOnly( First, Second, IncludeSeconds ) { return ( ( First.getHours() == Second.getHours() ) && ( First.getMinutes() == Second.getMinutes() ) && ( !IncludeSeconds || ( First.getSeconds() == Second.getSeconds() ) ) ); } function FormatEstimatedTime( StartTime ) { var Result = GetDisplayDate( StartTime ) + " " + GetDisplayTime( StartTime ); if ( StartTime.valueOf() <= 0 ) { return "Unknown"; } return Result; } function FormatBasicTime( StartTime ) { var Result = GetDisplayTime( StartTime, false ); if ( StartTime.valueOf() <= 0 ) { return "Unknown"; } return Result; } // Date Time display functions are used for Server and Client Side JScript // Returns dates in the form 'dd MMM yy' eg. 10 Jan 01 function FormatDateTime( StartTime ) { var Result = GetDisplayDate( StartTime ) + " " + GetDisplayTimeWithSeconds( StartTime ); if ( StartTime.valueOf() <= 0 ) { return "Unknown"; } return Result; } function GetDisplayDate( DateTime ) { if ( "" == DateTime ) { return ""; } var DateStr = new String(); var DateOnly = new Date( DateTime ); DateStr = DateOnly.getDate(); DateStr += " " + MonthNames[ DateOnly.getMonth() ]; var Year = DateOnly.getYear(); if ( Year < 1900 ) { Year += 1900; } DateStr += " " + Year; return DateStr; } function GetDisplayTimeCommon( DateTime, ShowSeconds, Round ) { var TimeStr = new String(); DateTime = new Date( DateTime ); if ( isNaN( DateTime ) ) { return ""; } var AmOrPm = ""; // Round the time if we're not displaying the seconds if ( !ShowSeconds && Round ) { if ( DateTime.getSeconds() >= 30 ) { DateTime.setMinutes( DateTime.getMinutes() + 1 ); } DateTime.setSeconds( 0 ); } var Hours = DateTime.getHours(); var Minutes = DateTime.getMinutes(); var Seconds = DateTime.getSeconds(); if ( Hours >= 12 ) { AmOrPm = "pm"; if ( Hours > 12 ) { Hours -= 12; } } else { if ( 0 == Hours ) { Hours = 12; } AmOrPm = "am"; } TimeStr += Hours + ":"; if ( Minutes < 10 ) { TimeStr += "0"; } TimeStr += Minutes; if ( ShowSeconds ) { // Add Seconds TimeStr += ":"; if ( Seconds < 10 ) { TimeStr += "0" } TimeStr += Seconds; } TimeStr += AmOrPm; return TimeStr; } // Returns times in the form 'hh:mm[am/pm]' eg. 1:04pm function GetDisplayTime( DateTime, Round ) { if ( null == Round ) { Round = true; } return GetDisplayTimeCommon( DateTime, false, Round ); } // Returns times in the form 'hh:mm:ss[am/pm]' eg. 1:04:23pm function GetDisplayTimeWithSeconds( DateTime ) { return GetDisplayTimeCommon( DateTime, true, false ); } // Works out a relative date based on the given pattern. // Valid values for Ordinal: // first, second, third, fourth, last // Valid values for Day: // day, weekday, weekend day, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday // e.g. to find the date of the second weekend day in January, 2004 // call RelativeDate( 0, 2004, "second", "weekend day" ); // (which should return Sunday 4 Jan, 2004) function RelativeDate( Month, Year, Ordinal, Day ) { // Check the Ordinal and Day values if ( null == Ordinals[ Ordinal ] ) { throw new Error( "Invalid Ordinal parameter supplied for RelativeDate" ); } if ( null == OrdinalMatchPattern[ Day ] ) { throw new Error( "Invalid Day parameter supplied for RelativeDate" ); } var StartDate = new Date( Year, Month, 1 ); if ( isNaN( StartDate ) ) { throw new Error( "Invalid Year or Month parameter supplied for RelativeDate" ); } var Direction = 1; var TargetCount = Ordinals[ Ordinal ]; if ( "last" == Ordinal ) { Direction = -1; // Move to the last day of the month instead StartDate.setMonth( Month + 1 ); StartDate.setDate( 1 ); StartDate.setDate( 0 ); } var TestDate = new Date( StartDate.valueOf() ); var Result = null; for ( var Count = 0; Count < TargetCount; ++Count ) { var MatchPatterns = OrdinalMatchPattern[ Day ]; var Matched = false; while ( !Matched && TestDate.getMonth() == Month ) { var CurrentDay = TestDate.getDay(); for ( var DayIndex in MatchPatterns ) { if ( MatchPatterns[ DayIndex ] == CurrentDay ) { Matched = true; Result = new Date( TestDate.valueOf() ); break; } } // See if the next day matches TestDate.setDate( TestDate.getDate() + Direction ); } } return Result; } // Gets the number of days, months, and years between two dates function TimeDifference( StartTime, EndTime ) { // Trim off the time values var Start = new Date( StartTime.getFullYear(), StartTime.getMonth(), StartTime.getDate() ); var End = new Date( EndTime.getFullYear(), EndTime.getMonth(), EndTime.getDate() ); // Swap the dates if the end is before the start if ( Start.valueOf() > End.valueOf() ) { var Temp = new Date( Start.valueOf() ); Start = new Date( End.valueOf() ); End = new Date( Temp.valueOf() ); } if ( Start.valueOf() == End.valueOf() ) { this.Days = 0; this.Months = 0; this.Years = 0; this.TotalDays = 0; } else { var mSecPerDay = 24 * 60 * 60 * 1000; this.TotalDays = Math.round( ( End.valueOf() - Start.valueOf() ) / mSecPerDay ); // Get the number of months between the dates var Mths = End.getMonth() - Start.getMonth() + ( End.getFullYear() - Start.getFullYear() ) * 12; if ( End.getDate() < Start.getDate() ) { --Mths; } var ApproxEnd = new Date( Start.valueOf() ); ApproxEnd.setMonth( ApproxEnd.getMonth() + Mths ); var RemainingDays = Math.round( ( End.valueOf() - ApproxEnd.valueOf() ) / mSecPerDay ); this.Days = RemainingDays; this.Months = Mths % 12; this.Years = Math.floor( Mths / 12 ); } } %>