1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099 |
- /*
- *+------------------------------------------------------------------------+
- *| Licensed Materials - Property of IBM
- *| BI and PM: prmt
- *| (C) Copyright IBM Corp. 2002, 2018
- *|
- *| US Government Users Restricted Rights - Use, duplication or
- *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- *|
- *+------------------------------------------------------------------------+
- */
- /*
- CDatePickerCommon.js
- This script provides utility functions for the date and dateTime
- prompt controls
- */
- //global variable
- var g_dNow = new Date();
- //get the current date
- //default to today's date
- var curYear = g_dNow.getFullYear();
- var curMonth = g_dNow.getMonth();
- var curDay = g_dNow.getDate();
- var PRMT_DateUtils = {
- 'mediumDateFormat' : null,
- 'mediumDateSeparator' : null
- };
- PRMT_DateUtils.dateRegexFromDateOrder = function(dateOrder) {
- var dateFormatReString;
- var dateSeparatorReString;
- var reDateParts;
- var datePartsOrder;
- if (dateOrder == 'MYD') {
- dateFormatReString = '([Mm]+.+[Yy]+.+[Dd]+)';
- reDateParts = '([Mm]+)(.+?)([Yy]+)[^Dd]+([Dd]+)';
- datePartsOrder = ['monthFormat','yearFormat','dayFormat'];
- } else if (dateOrder == 'MDY') {
- dateFormatReString = '^([Mm]+.+[Dd]+.+[Yy]+)';
- reDateParts = '([Mm]+)(.+?)([Dd]+)[^Yy]+([Yy]+)$';
- datePartsOrder = ['monthFormat','dayFormat','yearFormat'];
- } else if (dateOrder == 'YMD') {
- dateFormatReString = '([Yy]+.+[Mm]+.+[Dd]+)';
- reDateParts = '([Yy]+)(.+?)([Mm]+)[^Dd]+([Dd]+)';
- datePartsOrder = ['yearFormat', 'monthFormat','dayFormat'];
- } else if (dateOrder == 'YDM') {
- dateFormatReString = '([Yy]+.+[Dd]+.+[Mm]+)';
- reDateParts = '([Yy]+)(.+?)([Dd]+)[^Mm]+([Mm]+)';
- datePartsOrder = ['yearFormat','dayFormat', 'monthFormat'];
- } else if (dateOrder == 'DMY') {
- dateFormatReString = '([Dd]+.+[Mm]+.+[Yy]+)';
- reDateParts = '([Dd]+)(.+?)([Mm]+)[^Yy]+([Yy]+)';
- datePartsOrder = ['dayFormat','monthFormat','yearFormat'];
- } else if (dateOrder == 'DYM') {
- dateFormatReString = '([Dd]+.+[Yy]+.+[Mm]+)';
- reDateParts = '([Dd]+)(.+?)([Yy]+)[^Mm]+([Mm]+)';
- datePartsOrder = ['dayFormat', 'yearFormat', 'monthFormat'];
- }
- return { 'reDateFormat' : new RegExp(dateFormatReString), 'reDateParts': new RegExp(reDateParts), 'datePartsOrder': datePartsOrder };
- };
- //dateLength : medium | short
- PRMT_DateUtils.parseDateFormat = function (dateFormatToParse, dateOrder, defaultDateFormat, defaultDateSeparator, dateLenght) {
- var dateFormats = PRMT_DateUtils.dateRegexFromDateOrder(dateOrder);
- var extractedMatch = dateFormats['reDateFormat'].exec(dateFormatToParse);
- var dateFormat = defaultDateFormat, dateSeparator = defaultDateSeparator, datePartFormat = {};
- if (extractedMatch != null) {
- dateFormat = extractedMatch[1];
- //console.log("g_mediumFormat: \"" + g_mediumFormat + "\" dateFormat: \""+ dateFormat + "\"");
- extractedMatch = dateFormats['reDateParts'].exec(dateFormat);
- if (extractedMatch != null) {
- dateSeparator = extractedMatch[2];
- datePartFormat[dateFormats['datePartsOrder'][0]] = extractedMatch[1];
- datePartFormat[dateFormats['datePartsOrder'][1]] = extractedMatch[3];
- datePartFormat[dateFormats['datePartsOrder'][2]] = extractedMatch[4];
- //console.log("g_dateSeparatorMedium: \"" + g_dateSeparatorMedium + "\" dateSeparator: \""+ dateSeparator + "\"");
- //console.log("g_yearFormatMedium: \"" + g_yearFormatMedium + "\" mediumDatePartFormat.yearFormat: \""+ datePartFormat.yearFormat + "\"");
- }
- } else {
- //console.log("no match");
- }
- PRMT_DateUtils[dateLenght + 'DateFormat'] = dateFormat;
- PRMT_DateUtils[dateLenght + 'DateSeparator'] = dateSeparator;
- PRMT_DateUtils[dateLenght + 'DatePartFormat'] = datePartFormat;
- };
- // invoke this function when changing formats
- PRMT_DateUtils.resetDateFormats = function() { PRMT_DateUtils.mediumDateFormat = null; };
- //return a formatted text string based on a given day
- function getFormatDate(dSetDay, iType)
- {
- if (! PRMT_DateUtils.mediumDateFormat) {
- PRMT_DateUtils.parseDateFormat(g_mediumFormat, g_dateOrder, 'MMM d, yyyy', ' ','medium');
- PRMT_DateUtils.parseDateFormat(g_shortFormat, g_shortFormatDateOrder, "M/d/yy", '/','short');
-
- }
- if (!dSetDay)
- {
- return false;
- }
- //locale medium format
- var yearFormat = PRMT_DateUtils.mediumDatePartFormat.yearFormat; //g_yearFormatMedium;
- var monthFormat = PRMT_DateUtils.mediumDatePartFormat.monthFormat; // g_monthFormatMedium;
- var dayFormat = PRMT_DateUtils.mediumDatePartFormat.dayFormat; //g_dayFormatMedium;
- var sY = K_PRMT_sEMPTY;
- var sM = K_PRMT_sEMPTY;
- var sD = K_PRMT_sEMPTY;
- //0 = standard ; 1 = japaneseEmperor
- if (iType == 1)
- {
- sY = getJapaneseEra(dSetDay);
- }
- else
- {
- sY = dSetDay.getFullYear();
- sY = "0000" + sY;
- sY = sY.substring(sY.length-4, sY.length);
- //show as two digit year for those locales that require it
- if ((yearFormat == "yy") && (parseInt(sY, 10) > 1900))
- {
- sY = sY.substring(sY.length-2, sY.length);
- }
- }
- //get the formatted month
- if (monthFormat == "M")
- {
- sM = dSetDay.getMonth() + 1;
- }
- else if (monthFormat == "MMM")
- {
- sM = g_arMonthsAbbr[dSetDay.getMonth()];
- }
- else if (monthFormat == "MMMM")
- {
- sM = g_arMonths[dSetDay.getMonth()];
- }
- else
- {
- var iM = dSetDay.getMonth() + 1;
- sM = iM.toString();
- if (sM.length == 1)
- {
- sM = "0" + sM;
- }
- }
- if (dayFormat.match(/DD/i))
- {
- var iD = dSetDay.getDate();
- sD = iD.toString();
- if (sD.length == 1)
- {
- sD = "0" + sD;
- }
- }
- else
- {
- sD = dSetDay.getDate();
- }
- // show in medium date format
- sFormatDate = PRMT_DateUtils.mediumDateFormat;
- sFormatDate = sFormatDate.replace(RegExp(yearFormat, K_PRMT_sG), sY);
- sFormatDate = sFormatDate.replace(RegExp(dayFormat, K_PRMT_sG), sD);
- sFormatDate = sFormatDate.replace(RegExp(monthFormat, K_PRMT_sG), sM);
- return sFormatDate;
- }
- //return the number of days in the month
- function intGetDays(intMonth, intYear)
- {
- switch (intMonth)
- {
- case 0:
- return 31; // jan
- case 1:
- return (boolLeapYear(intYear))? 29:28; //feb
- case 2:
- return 31; //mar
- case 3:
- return 30;//apr
- case 4:
- return 31;//may
- case 5:
- return 30;//jun
- case 6:
- return 31;//jul
- case 7:
- return 31;//aug
- case 8:
- return 30;//sep
- case 9:
- return 31;//oct
- case 10:
- return 30;//nov
- case 11:
- return 31;//dec
- default:
- return -1; //problem determining month
- }
- }
- //return the text for a given month
- function sGetMonth(iMonth)
- {
- return g_arMonthsAbbr[iMonth];
- }
- //return the text for a given day
- function sGetDay(iDay)
- {
- return g_arDaysAbbr[iDay];
- }
- //check to see if this is a leap year
- /*if divisible evenly by 4, a Gregorian year is a leap year, with a February 29 and 366 days
- (e.g. 1996/4 = 499, so 1996 is a leap year), UNLESS
- if divisible evenly by 100, a Gregorian year is a normal year with 365 days
- (e.g.1900/100=19, so 1900 is a normal year of 365 days), UNLESS
- if divisible evenly by 400, a Gregorian year is a leap year;
- so the year 2000 is a leap year.
- */
- function boolLeapYear(iYear)
- {
- if ((iYear % 4 === 0) && ( (!(iYear % 100 === 0)) || (iYear % 400 === 0) ) )
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //return true if a string is a valid year
- function boolTestYear(sTestYear)
- {
- var reYear = new RegExp("^\\s*\\d{1,4}\\s*$");
- return ( typeof sTestYear == "string" && sTestYear.match(reYear) ? true : false );
- }
- //use this function to see if a string
- //can be turned into a valid date
- function boolTestDate(sTestDate)
- {
- var dTestDate = Date.parse(sTestDate);
- if (dTestDate) //this is a valid date
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //TODO
- function boolTestDateValidity(mdate)
- {
- if (mdate.getTime()) //this is a valid date
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //remove non alpha numeric data from a string
- function sStripNonAlphanumerics(sString)
- {
- var charDelimiters = /[\s,-]/g;
- var arTestDateStrip = sString.split(charDelimiters);
- var sTestDateStrip = arTestDateStrip.join(K_PRMT_sSP);
- return sTestDateStrip;
- }
- //parse string input and return a date object
- //return false if the value is not a date
- //if the input order is supplied, the parser
- //can determine ambiguous dates
- //ToDo: handle japanese days e.g. do any of the strings start with numbers, strip out day characters
- //ToDo: handle imperial time strings (this may need to happen prior to launching this function
- function dParseDate(sTestDate, sInputOrder)
- {
- //////////////////////
- // this logic should be able to properly parse any of the following typed in values
- //
- // jan 01 2001
- // june/05-2002
- // 29-MAY-1999
- // 01/02/2002 (requires inputOrder)
- // 2002-02-10 (requires inputOrder)
- // 2002/Feb/28
- //declare variables
- var iMonth = null;
- var iDay = null;
- var iYear = null;
- var bFoundDay = false;
- var iDayPos = null;
- var bFoundMonth = false;
- var iMonthPos = null;
- var bFoundYear = false;
- var iYearPos = null;
- //determine input order from locale
- //declare variables and default to international standard YMD
- var iYearExpectedPos = 0;
- var iMonthExpectedPos = 1;
- var iDayExpectedPos = 2;
- var rNoIntegers = /[^0-9]/;
- var myTestDate = null;
- //get the input order
- if (sInputOrder)
- {
- switch(sInputOrder)
- {
- case "MYD":
- iYearExpectedPos=1;
- iMonthExpectedPos=0;
- iDayExpectedPos=2;
- break;
- case "MDY":
- iYearExpectedPos=2;
- iMonthExpectedPos=0;
- iDayExpectedPos=1;
- break;
- case "YMD":
- iYearExpectedPos=0;
- iMonthExpectedPos=1;
- iDayExpectedPos=2;
- break;
- case "YDM":
- iYearExpectedPos=0;
- iMonthExpectedPos=2;
- iDayExpectedPos=1;
- break;
- case "DMY":
- iYearExpectedPos=2;
- iMonthExpectedPos=1;
- iDayExpectedPos=0;
- break;
- case "DYM":
- iYearExpectedPos=1;
- iMonthExpectedPos=2;
- iDayExpectedPos=0;
- break;
- default:
- iYearExpectedPos=0;
- iMonthExpectedPos=1;
- iDayExpectedPos=2;
- break;
- }
- }
- //strip out any extraneous characters
- //pass 1 (strip out the medium and short delimiters)
- var sTestDateReplace = sTestDate;
- var specialCharacters = "?.[]^$(){}|\\&";
- // g_dateSeparatorShort may not be a single character
- var dateSepShortArray = g_dateSeparatorShort.split("");
- var dateSepShort = "";
- for (var i = 0; i < dateSepShortArray.length; i++)
- {
- dateSepShort +=
- specialCharacters.indexOf(dateSepShortArray[i]) >= 0 ? ("\\" + dateSepShortArray[i]) : dateSepShortArray[i];
- }
- var rShortDateDelimiter = new RegExp(dateSepShort, K_PRMT_sG);
- sTestDateReplace = sTestDateReplace.replace(rShortDateDelimiter, K_PRMT_sSP);
- //Norwegian locale dates has ". " as mediumDateSeparator
- var norwegianSep = ". ";
- var mediumDateSeparator = PRMT_DateUtils.mediumDateSeparator; // old g_dateSeparatorMedium
- var dateSepMedium = ((specialCharacters.indexOf(mediumDateSeparator) >= 0)
- || (norwegianSep == mediumDateSeparator)) ? ("\\" + mediumDateSeparator) : mediumDateSeparator;
- var rMediumDateDelimiter = new RegExp(dateSepMedium, K_PRMT_sG);
- sTestDateReplace = sTestDateReplace.replace(rMediumDateDelimiter, K_PRMT_sSP);
- //pass 2 (strip out other known delimiters)
- var rDelimiters = /[\s,\-\/\\#\:]/g;
- //split the string into tokens
- var arDateTokens = sTestDateReplace.split(rDelimiters);
- //do we have enough tokens to proceed?
- if (arDateTokens.length != 3)
- {
- var rDelimitersWithDecimal = /[\s,\.\-\/\\#\:]/g;
- arDateTokens = sTestDateReplace.split(rDelimitersWithDecimal);
- }
- //check to see if there are enough tokens. (we need 3)
- if (arDateTokens.length < 3)
- {
- // we have less than three tokens,
- // the date is not valid
- return false;
- }
- else if (arDateTokens.length > 3)
- {
- //remove any empty array items
- //NS7 will create empty array items
- var arCleanArray = new Array();
- for (var x=0; x < arDateTokens.length; x++)
- {
- if (arDateTokens[x] !== K_PRMT_sEMPTY)
- {
- arCleanArray = arCleanArray.concat(arDateTokens[x]);
- }
- }
- //use the stripped array
- arDateTokens = arCleanArray;
- }
- if (arDateTokens.length > 3)
- {
- //we have more than three tokens
- //remove any character data and try again
- //this could be because of locale
- //for example, Japanese dates include symbols
- //strip any tokens with alpha numeric characters from the array of tokens
- var arStrippedArray = new Array();
- for (var i=0; i < arDateTokens.length; i++)
- {
- var z = arDateTokens[i].search(rNoIntegers);
- if (z == -1)
- {
- if (arDateTokens[i])
- {
- arStrippedArray = arStrippedArray.concat(arDateTokens[i]);
- }
- }
- }
- //check again
- //do we have 3 characters? if not error
- if (arStrippedArray.length == 3)
- {
- //use the stripped array
- arDateTokens = arStrippedArray;
- }
- else
- {
- //there is an incorrect number of tokens
- return false;
- }
- }
- for(var i = 0; i < arDateTokens.length; i++)
- {
- if(!validateStringForInvalidChars(arDateTokens[i]))
- {
- return false; // special chars in value
- }
- }
-
- //try to match the tokens according to input order
- //try and match year (medium and short formats)
- //what is the year position?
- if (arDateTokens[iYearExpectedPos] && arDateTokens[iYearExpectedPos].search(rNoIntegers) == -1)
- {
- if ((arDateTokens[iYearExpectedPos].length >= 1) && (arDateTokens[iYearExpectedPos].length <= 4))
- {
- iYear = arDateTokens[iYearExpectedPos];
- if (iYear.length == 2)
- {
- iYear = sShortToFullYearConversion(iYear);
- }
- bFoundYear = true;
- iYearPos= iYearExpectedPos;
- }
- }
- //try and match month (medium and short formats)
- //see if it matches the a localized month
- var sTestMonth = false;
- var iTestMonth = false;
- if (bNumbersOnly(arDateTokens[iMonthExpectedPos]) === true)
- {
- iTestMonth = parseInt(arDateTokens[iMonthExpectedPos],10) - 1;
- }
- else
- {
- sTestMonth = iCheckMonthString(arDateTokens[iMonthExpectedPos]);
- }
- if ((parseInt(sTestMonth,10) >= 0) && (parseInt(sTestMonth,10) <= 11))
- {
- iMonth = sTestMonth;
- bFoundMonth = true;
- iMonthPos = iMonthExpectedPos;
- }
- //see if it is a valid month by number
- else if (checkMonth(iTestMonth))
- {
- iMonth = iTestMonth;
- bFoundMonth = true;
- iMonthPos = iMonthExpectedPos;
- }
- //try and match day
- //we need a valid year and month to do this
- if ((bFoundMonth) && (bFoundYear))
- {
- if (checkDay(arDateTokens[iDayExpectedPos], parseInt(iMonth, 10), parseInt(iYear, 10)))
- {
- iDay = arDateTokens[iDayExpectedPos];
- bFoundDay = true;
- iDayPos = iDayExpectedPos;
- }
- }
- // do we have a valid date
- // if yes, update date
- if ((bFoundMonth) && (bFoundYear) && (bFoundDay))
- {
- // everything looks good, so use the javascript Date object to return a day
- myTestDate = prmt_createDateObject(iYear, iMonth, iDay);
- //set the year 'full' year explicitly
- myTestDate.setFullYear(iYear);
- if (!boolTestDateValidity(myTestDate))
- {
- //check on the date failed, return false
- return false;
- }
- else
- {
- //got a valid date, return it to the user
- return myTestDate;
- }
- }
- //The input did not match the locale's input order
- //perform a deeper analysis of the input
- bFoundYear = false;
- bFoundMonth = false;
- bFoundDay = false;
- iYear = null;
- iMonth = null;
- iDay = null;
- //try to match against SQL format
- iYearExpectedPos=0;
- iMonthExpectedPos=1;
- iDayExpectedPos=2;
- iYear = arDateTokens[0];
- iMonth = parseInt(arDateTokens[1],10)-1;
- iDay = parseInt(arDateTokens[2],10);
- //match year
- if ((bCheckFullYear(iYear)) && (checkMonth(iMonth)) && checkDay(iDay, iMonth, parseInt(iYear,10)))
- {
- myTestDate = new Date (iYear, iMonth, iDay);
- //set the year 'full' year explicitly
- myTestDate.setFullYear(iYear);
- return myTestDate;
- }
- //look for landmarks (such as a string month name or 4 digit year)
- //try to find the month (match against medium format) by string
- iYear = null;
- iMonth = null;
- iDay = null;
- var k=0;
- for (k=0; k < arDateTokens.length; k++)
- {
- var sSearchMonth = iCheckMonthString(arDateTokens[k]);
- if ((parseInt(sSearchMonth, 10) >= 0) && (parseInt(sSearchMonth, 10) <= 11))
- {
- iMonth = parseInt(sSearchMonth, 10);
- bFoundMonth = true;
- iMonthPos = k;
- }
- }
- //try to find the year (match 3 or 4 digits)
- var l=0;
- for (l=0; l < arDateTokens.length; l++)
- {
- if (bCheckFullYear(arDateTokens[l]) === true)
- {
- iYear= arDateTokens[l];
- bFoundYear = true;
- iYearPos = l;
- }
- }
- //do we have a month and year? try to assign day
- //found the year and month -- can we nail the day??
- if ((bFoundYear) && (bFoundMonth))
- {
- //grab the day
- var m = 0;
- for (m=0; m < arDateTokens.length; m++)
- {
- if ((m != iYearPos) && (m != iMonthPos))
- {
- if (arDateTokens[m].search(rNoIntegers) == -1)
- {
- //is this a valid day?
- if (checkDay(arDateTokens[m], parseInt(iMonth,10), parseInt(iYear,10)))
- {
- iDay = arDateTokens[m];
- bFoundDay = true;
- iDayPos= m;
- }
- }
- }
- }
- }
- // do we have a valid date
- // if yes, update date
- if ((bFoundMonth) && (bFoundYear) && (bFoundDay))
- {
- // everything looks good, so use the javascript Date object to return a day
- myTestDate = prmt_createDateObject(iYear, iMonth, iDay);
- //set the year 'full' year explicitly
- myTestDate.setFullYear(iYear);
- if (!boolTestDate(myTestDate))
- {
- //this is not a valid date, return false
- return false;
- }
- else
- {
- //got a valid date, return it to the user
- return myTestDate;
- }
- }
- else
- {
- // if no, error
- return false;
- }
- }
- // create a day object, shift the hour if DST change the date
- function prmt_createDateObject(iYear, iMonth, iDay) {
- var result = new Date (iYear, iMonth, iDay);
- var iHourShift = 1;
- while (result.getDate() != iDay && iHourShift < 6) {
- result = new Date(iYear, iMonth, iDay, iHourShift);
- iHourShift++;
- }
- return result;
- }
- //test to see if a string is a year
- function bCheckFullYear(sTestYear)
- {
- var rNoIntegers = /[^0-9]/;
- var z = sTestYear.search(rNoIntegers);
- if (z == -1)
- {
- if ((sTestYear.length == 3) || (sTestYear.length == 4))
- {
- return true;
- }
- }
- return false;
- }
- //Check to see if the passed in string contains regex chars, return true if none, false for an invalid string (contains regex special chars)
- function validateStringForInvalidChars(dateString)
- {
- //we can ignore '-', '.' and '/' here, as these are used in dates
- return !/[\[\]\{\}\(\)\*\+\?\\\^\$\|]/g.test(dateString);
- }
- //check to see if a string is a month
- function iCheckMonthString(sTestMonth)
- {
- //make the test month name the same size or smaller than the month
- //find the first match
- var rPeriod = new RegExp("\\.", K_PRMT_sGI);
- var sTestMonthNoDots = sTestMonth.replace(rPeriod, K_PRMT_sEMPTY);
- var j=0;
- for (j=0; j<g_arMonthsAbbr.length; j++)
- {
- //some locales use an optional '.' in the abbreviation
- //strip any periods off
- var sMonthCompare = g_arMonthsAbbr[j];
- sMonthCompare = sMonthCompare.replace(rPeriod,K_PRMT_sEMPTY);
- var iMonthLength = sMonthCompare.length;
- var sTestMonthTest = null;
- if (sTestMonthNoDots.length > iMonthLength)
- {
- sTestMonthTest = sTestMonthNoDots.substr(0, iMonthLength);
- }
- else
- {
- sTestMonthTest = sTestMonthNoDots;
- }
- //strip periods
- //sTestMonthTest = sTestMonthTest.replace(rPeriod, K_PRMT_sEMPTY);
- var myPattern = new RegExp("^" + sTestMonthTest, "i"); //ignore case
- //is there a match?
- if (sMonthCompare.match(myPattern))
- {
- //found a month, return it
- return j;
- }
- }
- return false;
- }
- //check month from a string
- function checkMonth(s)
- {
- var num = parseInt (s,10);
- if ((num >= 0) && (num <= 11))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //check the number of days in the month
- function checkDay(s, iTestMonth, iTestYear)
- {
- var iMaxDays = intGetDays(iTestMonth, iTestYear);
- var num = parseInt(s,10);
- return ((num >= 1) && (num <= iMaxDays));
- }
- function bIsDateLater(dDate, dTestDate)
- {
- //remove the time portions
- dDate.setHours(0);
- dDate.setSeconds(0);
- dDate.setMinutes(0);
- dDate.setMilliseconds(0);
- dTestDate.setHours(0);
- dTestDate.setSeconds(0);
- dTestDate.setMinutes(0);
- dTestDate.setMilliseconds(0);
- if (dDate > dTestDate)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- function bIsDateEarlier(dDate, dTestDate)
- {
- //remove the time portions
- dDate.setHours(0);
- dDate.setSeconds(0);
- dDate.setMinutes(0);
- dDate.setMilliseconds(0);
- dTestDate.setHours(0);
- dTestDate.setSeconds(0);
- dTestDate.setMinutes(0);
- dTestDate.setMilliseconds(0);
- if (dDate < dTestDate)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //japanese emperor time functions
- //create a japanese era object
- // sName: the name of the era
- // sStartYear: beginning year of the era
- // sStartMonth: beginning month of the era
- // sStartDay: beginning day of the era
- // sEndYear: end year of the era
- // sEndMonth: end month of the era
- // sEndDay: end day of the era
- function japaneseEra (sName, sStartYear, sStartMonth, sStartDay, sEndYear, sEndMonth, sEndDay)
- {
- this.m_sName = sName;
- this.m_dStartDate = new Date(sStartYear, (parseInt(sStartMonth,10) -1).toString(), sStartDay, 0, 0, 0);
- this.m_dEndDate = new Date(sEndYear, (parseInt(sEndMonth,10) -1).toString(), sEndDay, 23, 59, 59);
- }
- //return the japanese era from a date
- function getJapaneseEra (dTestDate)
- {
- var sJapaneseEra = K_PRMT_sEMPTY;
- updateJapaneseEraNames();
- for (var i = 0; i < japaneseEras.length; i++)
- {
- if (dTestDate >= japaneseEras[i].m_dStartDate && dTestDate <= japaneseEras[i].m_dEndDate)
- {
- sJapaneseEra = japaneseEras[i].m_sName;
- var eraYear = (parseInt(dTestDate.getFullYear(), 10) - parseInt(japaneseEras[i].m_dStartDate.getFullYear(), 10) +1);
- sJapaneseEra += K_PRMT_sSP + eraYear.toString();
- return sJapaneseEra;
- }
- }
- //this date did no match any known values, so return the current year
- sJapaneseEra = dTestDate.getFullYear();
- return sJapaneseEra;
- }
- function updateJapaneseEraNames()
- {
- if ( !(japaneseEras instanceof Array) || japaneseEras.length < 5)
- {
- return;
- }
- if (typeof PMT_EMP_JAPANESE_EMPORER_1 == K_PRMT_sSTRING)
- {
- japaneseEras[0].m_sName = PMT_EMP_JAPANESE_EMPORER_1;
- }
- if (typeof PMT_EMP_JAPANESE_EMPORER_2 == K_PRMT_sSTRING)
- {
- japaneseEras[1].m_sName = PMT_EMP_JAPANESE_EMPORER_2;
- }
- if (typeof PMT_EMP_JAPANESE_EMPORER_3 == K_PRMT_sSTRING)
- {
- japaneseEras[2].m_sName = PMT_EMP_JAPANESE_EMPORER_3;
- }
- if (typeof PMT_EMP_JAPANESE_EMPORER_4 == K_PRMT_sSTRING)
- {
- japaneseEras[3].m_sName = PMT_EMP_JAPANESE_EMPORER_4;
- }
- if (typeof PMT_EMP_JAPANESE_EMPORER_5 == K_PRMT_sSTRING)
- {
- japaneseEras[4].m_sName = PMT_EMP_JAPANESE_EMPORER_5;
- }
- }
- //function that will return the year from a given era
- function getYearfromEra (sEra, iEraYear)
- {
- updateJapaneseEraNames();
- for (var i=0; i < japaneseEras.length; i++)
- {
- if (sEra == japaneseEras[i].m_sName)
- {
- iEraYear = japaneseEras[i].m_dStartDate.getFullYear() + iEraYear - 1;
- return iEraYear;
- }
- }
- return false;
- }
- //this will test for a valid japanese era,
- //strip it out and then pass the value to the standard
- //date parser to finish the job
- function dParseEra(sTestDate, sInputOrder)
- {
- //split the string into an array based on non alphanumeric delimeters
- var rDelimiters = /[\s,\.-\/\\#\:]/g;
- var rNoIntegers = /[^0-9]/;
- var sEra = null;
- var iEraYear = null;
- var bFoundEra = false;
- var bFoundEraYear = false;
- var iEraPosition = null;
- var dEraDate = false;
- var arDateTokens = sTestDate.split(rDelimiters);
- updateJapaneseEraNames();
- //find the token with the era
- for (var i=0; i < arDateTokens.length; i++)
- {
- //there are characters in this token?
- if (arDateTokens[i].search(rNoIntegers) != -1)
- {
- //is it an era?
- for (var j=0; j < japaneseEras.length; j++)
- {
- var sTestEra = arDateTokens[i];
- if (sTestEra.length > japaneseEras[j].m_sName.length)
- {
- sTestEra = sTestEra.substring(0, japaneseEras[j].m_sName.length);
- }
- var myEra = new RegExp("^" + sTestEra, "i"); //ignore case
- //is there a match?
- if (japaneseEras[j].m_sName.match(myEra))
- {
- bFoundEra=true;
- iEraPosition = i;
- sEra = japaneseEras[j].m_sName;
- //is there a number to the right of the era?
- if (iEraPosition + 1 < arDateTokens.length )
- {
- //is it a number?
- //var rNoIntegers = /[^0-9]/;
- var y = arDateTokens[iEraPosition + 1].search(rNoIntegers);
- if (y == -1)
- {
- bFoundEraYear= true;
- iEraYearPosition = i+1;
- iEraYear = parseInt(arDateTokens[iEraPosition + 1],10);
- }
- }
- }
- }
- }
- }
- if ((bFoundEra === true) && (bFoundEraYear === true))
- {
- var sProcessedDate = K_PRMT_sEMPTY;
- var sConvertedEraYear = getYearfromEra(sEra, iEraYear);
- //concatenate back into a string
- for (var m=0; m < arDateTokens.length; m++)
- {
- if (m == iEraPosition)
- {
- //add the converted era to the string
- sProcessedDate += K_PRMT_sSP + sConvertedEraYear;
- }
- else if (m==iEraYearPosition)
- {
- //do nothing
- }
- else
- {
- //add to the end of the string
- sProcessedDate += K_PRMT_sSP + arDateTokens[m];
- }
- }
- dEraDate = dParseDate(sProcessedDate, sInputOrder);
- }
- //couldn't find a valid era, it might still be a valid date
- else
- {
- dEraDate = dParseDate(sTestDate, sInputOrder);
- }
- return dEraDate;
- }
- //test to a year to see if there is a corresponding
- //Imperial era
- function iTestEra(sTestYear)
- {
- var rDelimiters = /[\s,\.-\/\\#\:]/g;
- var arDateTokens = sTestYear.split(rDelimiters);
- var sTestEra = arDateTokens[0];
- var bFoundEra = false;
- var sFoundEra =K_PRMT_sEMPTY;
- var iFoundEraYear = null;
- if (arDateTokens.length == 2)
- {
- updateJapaneseEraNames();
- //is it an era?
- for (var j=0; j < japaneseEras.length; j++)
- {
- if (sTestEra.length > japaneseEras[j].m_sName.length)
- {
- sTestEra = sTestEra.substring(0, japaneseEras[j].m_sName.length);
- }
- var myEra = new RegExp("^" + sTestEra, "i"); //ignore case
- //is there a match?
- if (japaneseEras[j].m_sName.match(myEra))
- {
- bFoundEra = true;
- sFoundEra = japaneseEras[j].m_sName;
- iFoundEraYear = parseInt(arDateTokens[1],10);
- }
- }
- if (bFoundEra === true)
- {
- return getYearfromEra (sFoundEra, iFoundEraYear);
- }
- }
- return false;
- }
- //this function will convert 2 digit years to a 4 digit
- //year using the following convention:
- // 00-49 converted to 20**
- // 50-99 converted to 19**
- function sShortToFullYearConversion(sShortYear)
- {
- if (sShortYear.length == 2)
- {
- var iTestYear = parseInt(sShortYear,10);
- if (iTestYear >= MIN_YEAR && iTestYear <= MAX_YEAR)
- {
- iTestYear = iTestYear + 2000;
- }
- else
- {
- iTestYear = iTestYear + 1900;
- }
- var sConvertedYear = iTestYear.toString();
- return sConvertedYear;
- }
- else
- {
- //not a valid 2 digit year
- return false;
- }
- }
- // Default Date Arrays
- //these values are used if locale information is not available.
- g_arMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
- g_arMonthsAbbr = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
- g_arDays = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
- g_arDaysAbbr = new Array("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa");
- g_amString = "AM";
- g_pmString = "PM";
- g_startDayOfWeek = 1; // 1 = Sunday, 2 = Monday, etc...
- g_mediumFormat = "MMM d, yyyy h:mm:ss a";
- g_shortFormat = "M/d/yy h:mm a";
- g_dateOrder = "MDY";
- g_shortFormatDateOrder = "MDY";
- g_yearFormatMedium = "yyyy";
- g_monthFormatMedium = "MMM";
- g_dayFormatMedium = "d";
- g_hourFormatMedium = "h";
- g_minuteFormatMedium = "mm";
- g_secondFormatMedium = "ss";
- g_yearFormatShort = "yy";
- g_monthFormatShort = "M";
- g_dayFormatShort = "d";
- g_hourFormatShort = "h";
- g_minuteFormatShort = "mm";
- g_secondFormatShort = K_PRMT_sEMPTY;
- g_dateSeparatorMedium = K_PRMT_sSP;
- g_dateSeparatorShort = "/";
- g_timeSeparator = K_PRMT_sCOLON;
- var japaneseEras = new Array();
- japaneseEras[0] = new japaneseEra('Meiji', '1868', '8', '8', '1912', '7', '29');
- japaneseEras[1] = new japaneseEra('Taisho', '1912', '7', '30', '1926', '12', '24');
- japaneseEras[2] = new japaneseEra('Showa', '1926', '12', '25', '1989', '1', '7');
- japaneseEras[3] = new japaneseEra('Heisei', '1989', '1', '8', '2019', '4', '30');
- japaneseEras[4] = new japaneseEra('Reiwa', '2019', '5', '1', '2087', '12', '31');
- //2 digit year conversions
- MIN_YEAR = 0;
- MAX_YEAR = 49;
|