// ========================================================================== // theDate.js // by Walter Torres // Copyright 1998-2001 // World Wide Rights Reserved // // This script can be used for personal use only. // It CAN NOT be distributed without the express // written permission of the Auther. // // Please send comments, corrections and suggestions to // Walter Torres // ========================================================================== // ========================================================================== // This file contains new Functions for Dates: // var gDate = fromJulian( jDate ); # Returns the Gregorian Date from a Julian Date // var jDate = toJulian ( "10/21/1998" ); # Returns a Julian date from a Gregorian Date // var objDate = fromMilitaryTime( '1345' ); # Returns a Date Object from the given military time // var strDate = get_today(); # Returns a date string 'MM/DD/YYYY' from todays date // var strDataDate = makeDataDate ( strVal ); # Converts date string into 'Data' format YYYYMMDD // var strDate = convertToDate ( inputVal ); # Converts Data' format to Date Format 'MM/DD/YYYY' // blnValue = isDate ( inputVal ); # Validates a string for proper date // blnValue = isLeapYear ( year ); # Validates if given year is a Leap Year, or not // blnValue = ckDate ( mm, dd ); # Validates a string for proper day of a month // ========================================================================== // This file contains new Methods for DATE Object: // dateObject.theTime() Returns HH:MM:SS A|PM of the given Date Object // dateObject.theProgTime() Returns HHMMSS of the given Date Object // dateObject.clockTime(hh:mm:ss am) Returns a Date Object from givern time string // dateObject.getMilitaryTime() Returns HH:MM:SS of the given Date Object // dateObject.getTimeZone() Returns HH:MM:SS of the given Date Object // // dateObject.getTimeImage('path') Returns string for gif display of given time // // dateObject.theDate() Returns MM/DD/YYYY of the given Date Object // dateObject.theProgDate() Returns YYYYMMDD of the given Date Object // // dateObject.getJulian() Returns Julian date of the given Date Object // dateObject.fromJulian( jDate ) Returns a new Date Object from a given Julian date value // // dateObject.getWeekOf() Returns the date of the first day of that week // dateObject.getLongMonthName() Returns Full Name of the Month // dateObject.getShortMonthName() Returns Apprv. Name of the Month // dateObject.getLongDayName() Returns Full Name of the Day // dateObject.getShortDayName() Returns Apprv. Name of the Day // // dateObject.getDayOfYear() Returns Day of the Year of given Date Object // dateObject.getWeekOfYear() Returns Week of the Year of given Date Object // dateObject.getMonthOfYear() Returns Month of the Year of given Date Object // // dateObject.addDays(x) Adds given number of days to given Date Object // dateObject.addMonths(x) Adds given number of months to given Date Object // dateObject.addYears(x) Adds given number of years to given Date Object // // dateObject.addHours(x) Adds given number of Hours to given Date Object // dateObject.addMinutes(x) Adds given number of Minutes to given Date Object // dateObject.addSeconds(x) Adds given number of Seconds to given Date Object // // NOTE: These 'add' Methods also work with negative numbers! // ========================================================================== // ========================================================================== // New Methods for Date Functions Date.prototype.theDate = _getTheDate; Date.prototype.theProgDate = _getProgDate; Date.prototype.theTime = _getTheTime; Date.prototype.clockTime = _clockTime; Date.prototype.theProgTime = _getProgTime; Date.prototype.getMilitaryTime = _getMilTime; Date.prototype.getTimeZone = _getTimeZone; Date.prototype.getJulian = _getJulian; Date.prototype.fromJulian = _fromJulian; Date.prototype.getTimeImage = _getTimeImage; Date.prototype.getWeekOf = _getWeekOf; Date.prototype.getMonthLength = _getMonthLength; Date.prototype.getLongMonthName = _getLongMonthName; Date.prototype.getShortMonthName = _getShortMonthName; Date.prototype.getLongDayName = _getLongDayName; Date.prototype.getShortDayName = _getShortDayName; Date.prototype.getDayOfYear = _getDayOfYear; Date.prototype.getWeekOfYear = _getWeekOfYear; Date.prototype.getMonthOfYear = _getMonthOfYear; Date.prototype.addDays = _addDaysToDate; Date.prototype.addMonths = _addMonthsToDate; Date.prototype.addYears = _addYearsToDate; /* ========================================================================== New methods usage... // To use the new Month & Day Name methods // This will display: Thursday, August 13, 1998 var myDate = new Date("8/13/1998"); document.write ( myDate.getLongDayName() + ", " + myDate.getShortMonthName() + " " + myDate.getDate()+ ", " + myDate.getFullYear() ); // Also // To use the new .getWeekOfYear method // This will display: 33 var myDate = new Date("8/13/1998"); document.write ( "Week of the Year: " + myDate.getWeekOfYear() ); // ======================================================================= */ // Arrays to use for all Date/Time Functions var intDaysInMonth = new Array ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); var aryLongMonthName = new Array ( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ); var aryShortMonthName = new Array ( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" ); var aryLongDayName = new Array ( "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ); var aryShortDayName = new Array ( "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" ); var aryDateSuffix = new Array ( 'th', 'st', 'nd', 'rd', 'th' ); // ======================================================================= */ // New Calendar Object; different way to do the same thing... var Calendar = new Object; Calendar.daysInMonth = intDaysInMonth; Calendar.longMonthName = aryLongMonthName; Calendar.shortMonthName = aryShortMonthName; Calendar.longDayName = aryLongDayName; Calendar.shortDayName = aryShortDayName; Calendar.dateSuffix = aryDateSuffix; // ========================================================================== // ========================================================================== // Month Length // METHOD: Retrieve the number of days of the Month in a given Date Object // USAGE: intDaysInMonth = objDate.getMonthLength(); // INPUT: Date Object // OUTPUT: number of days in given month // by Walter Torres . function _getMonthLength() { // REMEMBER!: this.getMonth() is a base ZERO array! if ( ( this.getMonth() == 1 ) && isLeapYear ( this.getFullYear() ) ) return '29' return intDaysInMonth[this.getMonth()]; } // ========================================================================== // ========================================================================== // Month Names // METHOD: Retrieve the Long Name of the Month of the given Date Object // USAGE: strMonthName = objDate.getLongMonthName(); // INPUT: Date Object // OUTPUT: string containing the name of the month // by Walter Torres . function _getLongMonthName() { return aryLongMonthName[this.getMonth()]; } // METHOD: Retrieve the Short Name of the Month of the given Date Object // USAGE: strMonthName = objDate.getShortMonthName(); // INPUT: Date Object // OUTPUT: string containing the name of the month // by Walter Torres . function _getShortMonthName() { return aryShortMonthName[this.getMonth()]; } // ========================================================================== // Days of the Week Names // METHOD: Retrieve the Long Name of the Day of the given Date Object // USAGE: strDayName = objDate.getLongDayName(); // INPUT: Date Object // OUTPUT: string containing the name of the Day of the Week // by Walter Torres . function _getLongDayName() { return aryLongDayName[this.getDay()]; } // METHOD: Retrieves the Short Name of the Day of the given Date Object // USAGE: strDayName = objDate.getShortDayName(); // INPUT: Date Object // OUTPUT: string containing the name of the Day of the Week // by Walter Torres . function _getShortDayName() { return aryShortDayName[this.getDay()]; } // ========================================================================== // ========================================================================== // returns a new date that represents the first day of the week for the date. // By Scott Isaacs, www.insideDHTML.com // METHOD: Retrieves the date of the first day of the week for a given date // USAGE: strFirstDayWeek = objDate.getWeekOf(); // INPUT: Date Object // OUTPUT: string containing the date of the first day of the week '8/9/1998' function _getWeekOf() { sunday = ((arguments[0]==null) || (!arguments[0])); // Check for optional argument // COnvert to a Date Object var objTmpDate = new Date ( this - ((this.getDay() - ((sunday) ? 0 : 1)) *24*60*60*1000) ); // Send back just the date string return ( objTmpDate.theDate() ); } // ========================================================================== // ========================================================================== // returns the day of the year and week of the year. // Week by unknown posting to JavaScript NewsGroup. // modified for any Date by Walter Torres . // Day & Month by Walter Torres . // METHOD: Retrieves the Number Value for that Day of the Year for a given date // USAGE: intDayOfYear = objDate.getDayOfYear(); // INPUT: Date Object // OUTPUT: integer containing the Day of the Year for this date // by Unknown - modified by Walter Torres . function _getDayOfYear() { var strDate = new Date(this.getTime()); var strYear = new Date(strDate.getFullYear(),0,1); strYear = strYear.getTime() - (strYear.getDay()-1)*(24*60*60*1000); return(Math.ceil((strDate.getTime() - strYear)/(24*60*60*1000))); } // METHOD: Retrieves the Number Value for that Week of the Year for a given date // USAGE: intWeekOfYear = objDate.getWeekOfYear(); // INPUT: Date Object // OUTPUT: integer containing the Week of the Year for this date // by Unknown - modified by Walter Torres . function _getWeekOfYear() { var strDate = new Date(this.getTime()); var strYear = new Date(strDate.getFullYear(),0,1); strYear = strYear.getTime() - (strYear.getDay()-1)*(24*60*60*1000); return(Math.ceil((strDate.getTime() - strYear)/(7*24*60*60*1000))); } // METHOD: Retrieves the Number Value for that Month of the Year for a given date // USAGE: intMonthOfYear = objDate.getMonthOfYear(); // INPUT: Date Object // OUTPUT: integer containing the Month of the Year for this date // by Walter Torres . function _getMonthOfYear() { var strDate = this.getMonth() + 1; return(strDate) } // ========================================================================== // ========================================================================== // Add Day, Month & Year by Walter Torres . // These Methods are used to do Date calculations // NOTE: If you pass in a negative value, these Methods will do // date subtraction! // METHOD: Adds the given number of Days to given Date Object // and returns a new Date object. // USAGE: objNewDate = objDate.AddDaysToDate( intValue ); // INPUT: Date Object & number value to add (or subtract) // OUTPUT: Date Object with date forward (or backward) from given date // by Walter Torres . function _addDaysToDate( inputVal ) { // See if we got anything if ( ! inputVal ) return this // Retrieve the Date pieces var strMonth = this.getMonth(); var strDay = this.getDate() + new Number ( inputVal ); var strYear = this.getFullYear(); // Retrieve the Time pieces var strHour = this.getHours(); var strMinutes = this.getMinutes(); var strSeconds = this.getSeconds(); // Create a new Date Object return new Date( strYear, strMonth, strDay, strHour, strMinutes, strSeconds ) } // METHOD: Adds the given number of Months to given Date Object // and returns a new Date object. // USAGE: objNewDate = objDate.addMonths( intValue ); // INPUT: Date Object & number value to add (or subtract) // OUTPUT: Date Object with date forward (or backward) from given date // by Walter Torres . function _addMonthsToDate( inputVal ) { // See if we got anything if ( ! inputVal ) return this // Retrieve the Date pieces var strMonth = this.getMonth() + new Number ( inputVal ); var strDay = this.getDate(); var strYear = this.getFullYear(); // Retrieve the Time pieces var strHour = this.getHours(); var strMinutes = this.getMinutes(); var strSeconds = this.getSeconds(); // Create a new Date Object return new Date( strYear, strMonth, strDay, strHour, strMinutes, strSeconds ) } // METHOD: Adds the given number of Years to given Date Object // and returns a new Date object. // USAGE: objNewDate = objDate.AddYearsToDate( intValue ); // INPUT: Date Object & number value to add (or subtract) // OUTPUT: Date Object with date forward (or backward) from given date // by Walter Torres . function _addYearsToDate( inputVal ) { // See if we got anything if ( ! inputVal ) return this // Retrieve the Date pieces var strMonth = this.getMonth(); var strDay = this.getDate(); var strYear = this.getFullYear() + new Number ( inputVal ); // Retrieve the Time pieces var strHour = this.getHours(); var strMinutes = this.getMinutes(); var strSeconds = this.getSeconds(); // Create a new Date Object return new Date( strYear, strMonth, strDay, strHour, strMinutes, strSeconds ) } // ========================================================================== // ========================================================================== // Add Hours, Minutes & Seconds by Walter Torres . // These Methods are used to do Time calculations // METHOD: Adds the given number of Hours to given Date Object // and returns a new Date object. // USAGE: objNewDate = objDate.AddHoursToDate( intValue ); // INPUT: Date Object & number value to add (or subtract) // OUTPUT: Date Object with date forward (or backward) from given date // by Walter Torres . function _AddHoursToDate( inputVal ) { // See if we got anything if ( ! inputVal ) return this // Retrieve the Date pieces var strMonth = this.getMonth() + 1; var strDay = this.getDate(); var strYear = this.getFullYear(); // Retrieve the Time pieces var strHour = this.getHours() + new Number ( inputVal ); var strMinutes = this.getMinutes(); var strSeconds = this.getSeconds(); // Create a new Date Object return new Date( strYear, strMonth, strDay, strHour, strMinutes, strSeconds ) } // METHOD: Adds the given number of Minutes to given Date Object // and returns a new Date object. // USAGE: objNewDate = objDate.AddMinutesToDate( intValue ); // INPUT: Date Object & number value to add (or subtract) // OUTPUT: Date Object with date forward (or backward) from given date // by Walter Torres . function _AddMinutesToDate( inputVal ) { // See if we got anything if ( ! inputVal ) return this // Retrieve the Date pieces var strMonth = this.getMonth() + 1; var strDay = this.getDate(); var strYear = this.getFullYear(); // Retrieve the Time pieces var strHour = this.getHours(); var strMinutes = this.getMinutes() + inputVal; var strSeconds = this.getSeconds(); // Create a new Date Object return new Date( strYear, strMonth, strDay, strHour, strMinutes, strSeconds ) } // METHOD: Adds the given number of Seconds to given Date Object // and returns a new Date object. // USAGE: objNewDate = objDate.AddSecondsToDate( intValue ); // INPUT: Date Object & number value to add (or subtract) // OUTPUT: Date Object with date forward (or backward) from given date // by Walter Torres . function _AddSecondsToDate( inputVal ) { // See if we got anything if ( ! inputVal ) return this // Retrieve the Date pieces var strMonth = this.getMonth() + 1; var strDay = this.getDate(); var strYear = this.getFullYear(); // Retrieve the Time pieces var strHour = this.getHours(); var strMinutes = this.getMinutes(); var strSeconds = this.getSeconds() + new Number ( inputVal ); // Create a new Date Object return new Date( strYear, strMonth, strDay, strHour, strMinutes, strSeconds ) } // ========================================================================== // ========================================================================== // Get the Date & Time by Walter Torres . // METHOD: Returns the date (in US FORMAT MM/DD/YYY) of a given Date object. // USAGE: strDate = objDate.getTheDate( ); // INPUT: Date Object // OUTPUT: String of date in US Format (MM/DD/YYYY) // by Walter Torres . function _getTheDate() { // Retrieve the Date pieces var strMonth = this.getMonth() + 1; var strDay = this.getDate(); var strYear = this.getFullYear(); // Create a new Date Object return strMonth + '/' + strDay + '/' + strYear } // METHOD: Returns the date (YYYYMMDD) of a given Date object // for use in programming and sorting. // USAGE: strDate = objDate.getProgDate( ); // INPUT: Date Object // OUTPUT: String of date in Programming Format (YYYYMMDD) // by Walter Torres . function _getProgDate() { // Retrieve the Date pieces var strMonth = this.getMonth() + 1; var strDay = this.getDate(); var strYear = this.getFullYear(); // Return the new Date string // the '' is used to make sure this is a string and // not ADDED together for some number return strYear + '' + strMonth + '' + strDay } // ========================================================================== // METHOD: Returns the time (HH:MM:SS AM|PM) of a given Date object. // USAGE: strTime = objDate.getTheTime( ); // INPUT: Date Object // OUTPUT: String of the Time (HH:MM:SS AM|PM) // by Walter Torres . function _getTheTime() { // Retrieve and build time variables var intHour = this.getHours(); // Get Hour var intCurMinute = this.getMinutes(); // Get Minutes var intCurSecond = this.getSeconds(); // Get Seconds var strTimeZone = this.getTimeZone(); // Get Time Zone var strCurHour = ((intHour > 12) ? intHour - 12 : intHour); // fix military time strCurHour = ((strCurHour == 0) ? strCurHour = 12 : strCurHour); // fix midnight time strCurHour = ((strCurHour < 10) ? '0' + strCurHour : strCurHour); // Add ZERO, maybe intCurMinute = ((intCurMinute < 10) ? '0' + intCurMinute : intCurMinute); // Add ZERO, maybe intCurSecond = ((intCurSecond < 10) ? '0' + intCurSecond : intCurSecond); // Add ZERO, maybe var strAmpm = ( intHour >= 12) ? "pm" : "am"; // determine PM or PM // Build the text string strCurTime = strCurHour + ":" + intCurMinute + ":" + intCurSecond + " "; strCurTime += strAmpm + " " + strTimeZone; // Return the new string return strCurTime; // the time string 'HH:MM:SS AM|PM' } // METHOD: Returns the time (HH:MM:SS AM|PM) of a given Date object. // for use in programming and sorting. // USAGE: strTime = objDate.getProgTime( ); // INPUT: Date Object // returnLength: 1 to 6, the time is truncated to that length. // 2 - HH, 4 - HHMM, 6 - HHMMSS (default) // OUTPUT: String of time in Programming Format (HHMMSS) // by Walter Torres . function _getProgTime( returnLength ) { // If we were not given a value, set it if ( ! returnLength || returnLength > 6 ) returnLength = 6; // Retrieve Military Time and convert it to a String var milTime = this.getMilitaryTime() + ''; milTime = milTime.replace ( /:/g, '' ); // strip out the colons // return only the numbers return milTime.substring ( 0, returnLength ) } // ========================================================================== // METHOD: Returns the time in Military Format (HH:MM:SS) of a given Date object. // for use in programming and sorting. // USAGE: strMilTime = objDate.getMilitaryTime( ); // INPUT: Date Object // OUTPUT: String of time Military Format (HH:MM:SS) // by Walter Torres . function _getMilTime() { // Retrieve and build time variables var intHour = this.getHours(); // Get Hour var intMinute = this.getMinutes(); // Get Minutes var intSecond = this.getSeconds(); // Get Seconds var strTimeZone = this.getTimeZone(); // Get Time Zone var intHour = ((intHour < 10) ? '0' + intHour : intHour); // Add ZERO, maybe intMinute = ((intMinute < 10) ? '0' + intMinute : intMinute); // Add ZERO, maybe intSecond = ((intSecond < 10) ? '0' + intSecond : intSecond); // Add ZERO, maybe // Build the text string return intHour + ":" + intMinute + ":" + intSecond + " "+ strTimeZone; } // FUNCTION: Returns a new DATE Object from a given value // in Miltary Time format (HHMMSS). // USAGE: objNewDate = fromMilitaryTime( '1345' ); // INPUT: milTime - time value in military format (HH:MM:SS) // OUTPUT: New Date Object with time converted from Military Format // by Walter Torres . // // NOTE: This function will accept the time format with or with the colons function fromMilitaryTime ( milTime ) { // See if we recieved anything if ( ! milTime ) return null // Convert input to a String so we can pick it apart milTime = new String ( milTime ); // Remove the colons milTime = milTime.replace ( /:/g, ''); // Pull apart the string // NOTE: Converts the value to a number intHour = new Number ( milTime.substr( 0,2 ) ); intMinute = new Number ( milTime.substr( 2,2 ) ); intSecond = new Number ( milTime.substr( 4,2 ) ); // Build the text string return new Date( 0, 0, 1, intHour, intMinute, intSecond ) } // METHOD: Converts a given time string (hh:mm:ss am) into a Date Object // USAGE: objDate.clockTime( '11:32:45 am' ); // INPUT: Date Object // Clock Time string (11:32:45 am) // Seconds and the space are optional, but the AM/PM is not! // OUTPUT: Date Object converting input. // by Walter Torres . // // NOTE: You can use this Method to convert data from a time pick list. function _clockTime ( clockTime ) { // force any text to lower case clockTime.toLowerCase(); // Tear apart the Clock string into its pieces // We are looking for... // 1 or 2 digits in the HOUR // 1 or 2 digits in the MINUTE // 1 or 2 digits in the SECONDS (optional input) // 0 or more spaces // and 'A' or a 'P' only. we don't care about the 'M' var regEx = /^(\d*):(\d*):?(\d*)\s*([AP])m?/i; var myResults = clockTime.match( regEx ); // See if we have a valid format and/or did we find anything // If not, we return nothing, this does not change the Date Object if ( ! myResults ) return null var lvl = myResults[4]; var hr = new Number ( myResults[1] ); var min = new Number ( myResults[2] ); var sec = new Number ( myResults[3] ); // If we have PM if ( lvl == 'p' ) { // If this is not NOON if ( hr != 12 ) { // Add 12 to the hour to make it 24 hour clock value hr = hr + 12; } // If this is NOON, we don't have to do anything } // This is AM, see if we are at MIDNIGHT else if ( hr == 12 ) { // MIDNIGHT has a ZERO hour value hr = 0; } // Set time into this Date Object this.setHours( hr ); this.setMinutes( min ); this.setSeconds( sec ); } // function mil_to_index ( mil_time ) // ==================================================================== // ========================================================================== // METHOD: Returns TimeZone set on local machine. // USAGE: strTimeZone = objDate.getTimeZone( ); // INPUT: Date Object // OUTPUT: String of Time Zone (CST) // by Walter Torres . function _getTimeZone() { // Convert what is sent in to a Date Object var strDate = new Date(this); // Convert this Date Object to a String strDate = strDate.toString(); // Split the Date/Time String into an Array strDate = strDate.split(" "); // Send back the Time Zone return strDate[4] } // ========================================================================== // ========================================================================== // Method to Create a HTML string to display a given time as images // by Walter Torres // METHOD: Returns string for HTML code for GIF display of given Time. // USAGE: strTimeImage = objDate.getTimeImage( strImgPath ); // INPUT: Date Object // strImgPath: path to GIF images // OUTPUT: HTML code for time display // by Walter Torres . function _getTimeImage ( strImgPath ) { /* Builds HTML to display the time in images This accepts: strCurTime: a time string to parse. format: 'HH:MM:SS AM|PM' strImgPath: path to where the images are This returns: strImgTime: string of HTML code for display time images NOTE: The Image Path needs to include a file prefix i.e. '../images/numbers/LCD-Blue/' We assume that these are GIF files. We assume an image file name structure: dg-0.gif - digit ZERO ... - the other numbers dg-9.gif - digit NINE dg-b.gif - blank space dg-c.gif - Colon ':' dg-a.gif - A dg-p.gif - P dg-m.gif - M */ // See if we got anything if ( ! strImgPath ) return 'Please indicate Path to Images.'; // Get the Date Object sent to us var strCurTime = this.theTime(); // Refesh this variable each time through var strImgTime = ''; // Build HTML image tag code from path variable var OpenImg = ''; } return strImgTime // string of HTML code for display time image } // ========================================================================== // ========================================================================== // Methods & Functions to handle Julian <-> Gregorian Converstions // by Walter Torres // based on Perl code by Gary Puckering // ========================================================================== // FUNCTION: Converts a Julian value into a Gregoian date format (MM/DD/YYYY) // USAGE: var gDate = fromJulian( jDate ); // INPUT: jd - Julian Date value // OUTPUT: String with converted Gregoian date (MM/DD/YYYY) function fromJulian ( jd ) { var jdate_tmp; var m, d, y; jdate_tmp = jd - 1721119; y = parseInt ( (4 * jdate_tmp - 1 ) / 146097 ); jdate_tmp = 4 * jdate_tmp - 1 - 146097 * y; d = parseInt ( jdate_tmp / 4 ); jdate_tmp = parseInt ( ( 4 * d + 3 ) / 1461 ); d = 4 * d + 3 - 1461 * jdate_tmp; d = parseInt( ( d + 4 ) / 4 ); m = parseInt ( ( 5 * d - 3 ) / 153 ); d = 5 * d - 3 - 153 * m; d = parseInt ( ( d + 5 ) / 5 ); y = 100 * y + jdate_tmp; if ( m < 10 ) { m += 3; } else { m -= 9; ++y; } return m + '/' + d + '/' + y } // fromJulian ( jd ) // ========================================================================== // METHOD: Returns a Date Object based upon a given Julian Date. // USAGE: var objNewDate = objDate.fromJulian ( julianValue ); // INPUT: Date Object // jDate: Julian Date Value // OUTPUT: New Date Object converted from given Julian date function _fromJulian ( jDate ) { // We were not sent a value, so return the existing Date Object if ( ! jDate ) return this // Call the fromJulian function to perform this action // and return a new Date Object value return new Date ( fromJulian( jDate ) ) } // _fromJulian ( jDate ) // ========================================================================== // FUNCTION: Converts Gregoian date format (MM/DD/YYYY) to Julian value // USAGE: var julian_date = toJulian ( "10/21/1998" ); // INPUT: inputDate // OUTPUT: Julian Date value // NOTE: If no date is given, uses current System Date. function toJulian ( inputDate ) { if ( ! inputDate ) { // No value given, so use todays Date var myDate = new Date(); } else { // Create a Date Object with given value var myDate = new Date( inputDate ); } // Convert Date Object into Julian date Format using out Method return myDate.getJulian() } // toJulian ( inputDate ) // ========================================================================== // FUNCTION: Retrieves the Julian Date from a given Date Object // USAGE: var julian_date = objDate.getJulian(); // INPUT: Date Object // OUTPUT: Julian Date value function _getJulian ( ) { // Retrieve the Date pieces var m = this.getMonth() + 1; // ZERO based function var d = this.getDate(); var y = this.getFullYear(); // Do some math if (m > 2) { m -= 3; } else { m += 9; --y; } // Do some more math var c = parseInt ( y / 100 ); var ya = y - ( 100 * c ); var jd = parseInt ( ( 146097 * c ) / 4 ) + parseInt ( ( 1461 * ya ) / 4 ) + parseInt ( ( 153 * m + 2 ) / 5 ) + d + 1721119; // Send back the Julian Date value from the given Date Object return jd } // _getJulian ( ) // ========================================================================== // ========================================================================== // FUNCTION: Converts a ISO 8601 Date string into a Date Object // USAGE: var objDate = fromISO8601 ( strISO8601Date ); // INPUT: ISO 8601 Date string // OUTPUT: Julian Date value function fromISO8601 ( strISO ) { // See if we were sent a value, if not bale if ( ! strISO ) return this // Error var, if we need it! var errorMsg = ''; // 2001-01-17T02:30:24 // We need to see if we have a valid ISO 8601 structure // First, see if we have the minumum length // 4 year, dash, 2 month, dash, 2 day, 'T', // 2 hour, colon, 2 minute, colon, 2 second, // Optional: 3 digit seconds, 1 TZ if ( inputVal.length < 19 ) errorMsg += '\n Not enough numbers.'; // Also make sure we don't have too many characters if ( inputVal.length > 23 ) errorMsg += '\n Too many numbers.'; } // ========================================================================== // ========================================================================== // ==================================================================== // Functions // ==================================================================== // Builds a Date String, 'MM/DD/YYYY' from todays date // INPUT: none // OUTPUT: a Date String, 'MM/DD/YYYY' from todays date // DEPENDANCIES: none // by Walter Torres . function get_today( ) { new_date = new Date( ); return ( new_date.getMonth() + 1 ) + '/' + new_date.getDate() + '/' + new_date.getFullYear() } // get_today( ) // ==================================================================== // ==================================================================== // Check a given string to see if it is a valdate date // INPUT: inputVal = MM/DD/YY(YY) format string // OUTPUT: TRUE or FALSE // DEPENDANCIES: ckDate() // by Walter Torres . // // NOTE: This returns one of 2 types of Objects; Boolean or String. // A conditioanl check must be added after a call to this // function to make a decision based upon what is returned. // // EXAMPLE: var myDate = ckDate ( month, day, year ) // // See if we have any problems // if ( ( typeof ( myDate ) ) == 'string' ) // alert ( objXMLdata ); // function isDate ( inputVal ) { // Error var, if we need it! var errorMsg = ''; // First, see if we have the minumum length // 2 month, 2 day, 2 year, 0 delimiters = 6 characters // NOTE: If no delimiters, then we need 2 digits for month and days if ( inputVal.length < 6 ) errorMsg += '\n Not enough numbers.'; // Also make sure we don't have too many characters // 2 month, 2 day, 4 year, 2 delimiters = 10 characters if ( inputVal.length > 10 ) errorMsg += '\n Too many numbers.'; // If we have any dashes in here, convert them to slashes // Also check for back slash and periods. var regEx = /[.\-\\]/gi; // Perform a regular expression to replace found characters inputVal = inputVal.replace(regEx, "/"); // Check if the overall formating is OK var delim_1 = inputVal.indexOf('/'); var delim_2 = inputVal.lastIndexOf('/'); // See if there is more than one delimiter in this value if ( delim_1 != -1 && delim_1 == delim_2 ) errorMsg += '\n Invalid delimiters.'; // Since we have delimiters, parse string this way if ( delim_1 != -1 ) { var mm = parseInt ( inputVal.substring ( 0, delim_1 ), 10 ) var dd = parseInt ( inputVal.substring ( delim_1 + 1, delim_2 ), 10 ) var yy = parseInt ( inputVal.substring ( delim_2 + 1, inputVal.length ), 10 ) } // Or, we don't have delimiters, parse this way else { var mm = parseInt ( inputVal.substring ( 0, 2 ), 10 ) var dd = parseInt ( inputVal.substring ( 2, 4 ), 10 ) var yy = parseInt ( inputVal.substring ( 4, inputVal.length ), 10 ) } // parseInt converts to numbers, so check if any are not numbers if ( isNaN(mm) || isNaN(dd) || isNaN(yy) ) errorMsg += '\n Only numbers please.'; // Return the error message or an OK (TRUE) if (errorMsg != '') return 'Error:' + errorMsg; // Otherwise... // Make sure the Date is a valid value // Now that we have played with this given string, // lets see if it really is a vaild date! // Whatever comes back, we are just going to pass it on! return ckDate ( mm, dd, yy ) } // isDate ( inputVal ) // ==================================================================== // ==================================================================== // Check a given string to see if it is a valdate day of a given month // INPUT: mm = 1 or 2 digit for a month // dd = 1 or 2 digit for a day of the month // yy = 2 or 4 digit for a year // OUTPUT: TRUE or ERROR Msg // DEPENDANCIES: isLeapYear() // aryLongMonthName array // by Walter Torres . // // NOTE: This returns one of 2 types of Objects; Boolean or String. // A conditioanl check must be added after a call to this // function to make a decision based upon what is returned. // // EXAMPLE: var myDate = ckDate ( month, day, year ) // // See if we have any problems // if ( ( typeof ( myDate ) ) == 'string' ) // alert ( myDate ); // function ckDate ( mm, dd, yy ) { // Error var, if we need it! var errorMsg = ''; // None have more than 31 or less than 1 if ( (dd < 1) || (dd > 31) ) errorMsg += '\n Invalid Day value.'; // Make sure we have proper month values if ( (mm < 1) || (mm > 12) ) errorMsg += '\n Invalid Month value.'; // April, June, August, November hath 30 else if ( ( mm == 4 || mm == 6 || mm == 9 || mm == 11 ) && dd > 30 ) errorMsg += '\n ' + aryLongMonthName[mm-1] + ' does not have ' + dd + ' days.'; // Check Feburary, this does[!] taken in account Leapyears! else if ( ( mm == 2 ) && ( dd > 29 || ( dd == 29 && (!isLeapYear ( yy )) ) ) ) errorMsg += '\n ' + yy + ' is not a Leap Year!'; // Return the error message or an OK (TRUE) if (errorMsg != '') return 'Error:' + errorMsg; // Otherwise this date is fine! return true } // ckDate ( mm, dd ) // ==================================================================== // ==================================================================== // Check a given string to see if it is a valdate day of a given month // INPUT: yy = 2 or 4 digit year // OUTPUT: TRUE or FALSE // by Unknown - modified by Walter Torres . function isLeapYear ( yy ) { return ( ( (yy % 4) == 0 ) && ( ( (yy % 100) != 0 ) || ( (yy % 400) == 0 ) ) ) } // isLeapYear ( yy ) // ==================================================================== // ==================================================================== // This does two things... // 1) Validates the input string for a validate date string // 2) Converts to a Data Date String, i.e YYYYMMDD // INPUT: mm = 1 or 2 digit for a month // dd = 1 or 2 digit for a day of the month // OUTPUT: a Data Date String, i.e YYYYMMDD or ERROR Msg // DEPENDANCIES: none // by Walter Torres . function makeDataDate ( inputVal ) { // Error var, if we need it! var errorMsg = ''; // First, see if we have the minumum length // if we assume 2 digits per month, day & year // and no delimiter, then we need 6 characters if ( inputVal.length < 6 ) errorMsg += '\n Not Enough Characters.'; // If we have any dashes in here, convert them to slashes // Also check for back slash and periods. var regEx = /[.\-\\]/gi; // Perform a regular expression to replace found characters inputVal = inputVal.replace(regEx, "/"); // Check if the overall formating is OK var delim_1 = inputVal.indexOf('/'); var delim_2 = inputVal.lastIndexOf('/'); // See if there is more than one delimiter in this value if ( delim_1 != -1 && delim_1 == delim_2 ) errorMsg += '\n Too Many Delimiters'; // Since we have delimiters, parse string this way if ( delim_1 != -1 ) { var mm = parseInt ( inputVal.substring ( 0, delim_1 ), 10 ); var dd = parseInt ( inputVal.substring ( delim_1 + 1, delim_2 ), 10 ); var yy = parseInt ( inputVal.substring ( delim_2 + 1, inputVal.length ), 10 ); } // Or, we don't have delimiters, parse this way else { var mm = parseInt ( inputVal.substring ( 0, 2 ), 10 ); var dd = parseInt ( inputVal.substring ( 2, 4 ), 10 ); var yy = parseInt ( inputVal.substring ( 4, inputVal.length ), 10 ); } // parseInt converts to numbers, so check if any are not numbers if ( isNaN(mm) || isNaN(dd) || isNaN(yy) ) errorMsg += '\n Found an non-Numeric value'; // Return the error message or an OK (TRUE) if (errorMsg != '') return 'Error:' + errorMsg; // Otherwise... // Make sure the Date is a valid value // Now that we have played with this given string, // lets see if it really is a vaild date! // Whatever comes back, we are just going to pass it on! var check = ckDate ( mm, dd, yy ); if ( ( typeof ( check ) ) == 'string' ) return check // Convert the Day number into a String day = new String ( dd ); // Add a leading ZERO to make it a 2 digit Day if ( day.length < 2 ) day = '0' + day; // Convert the Month number into a String mon = new String ( mm ); // Add a leading ZERO to make it a 2 digit Month if ( mon.length < 2 ) mon = '0' + mon; // Convert any 2 digit year to a 4 digit year // Big assumtion here, anything between 70 and 99 is a 1900 year // the rest is a 2000 year yy += ( (yy > 70) && (yy < 100) ) ? 1900 : 2000; // OK, we got this far, so we must be OK return yy + mon + day } // makeDataDate ( strVal ) // ==================================================================== // ==================================================================== // This Converts a 'Data' Date String to a Date String, i.e MM/DD/YYYY // INPUT: inputVal = YYYYMMDD // OUTPUT: string = MM/DD/YYYY // DEPENDANCIES: none // by Walter Torres . function convertToDate ( inputVal ) { // First, see if we have the minumum length // if we assume 2 digits per month, day & 4 digits for year // and no delimiter, then we need 8 characters if ( inputVal.length < 8 ) // Define why and bale return 'Not Enough Characters' // Parse string into components return (inputVal.substring ( 6, 4 )) + '/' + (inputVal.substring ( 8, 6 )) + '/' + (inputVal.substring ( 0, 4 )) } // convertToDate ( inputVal ) // ==================================================================== // eof