Var MonthNames = ["January", "February", "March", "l", "May", "June", "July", "August", "September ", "October", "November", "December"]; Var DayNames = ["Sunday", "Monday", "Tueday", "Wednesday", "Thursday ", "Friday", "Saturday"]; Var ShortMths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug ", "Sep", "Oct", "Nov", "Dec"]; Var Expiration days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; Var StringToDate = function (sDate, sFormat, cutOff ){ // Input: a date value as a string, it's format as a string e.g. 'dd-mmm-yy' // Optional: a cutoff (integer) for 2 digit years. // If no 'D' appears in the format string then the 1st of the month is assumed. // If the year is 20 and the cut-off is 30 then the value will be converted // To 2020; if the year is 40 then this will be converted to 1940. // If no cut-off is supplied then '20' will be pre-pended to the year (YY ). // Output: a string in the format 'yyyy/MM/dd' or'' // Will not attempt to convert certain combinations e.g. DMM, MDD, DDM, YYYYD. Var sParsed, fndSingle; // SParsed will be constructed in the format 'yyyy/MM/dd' SDate = sDate. toString (). toUpperCase (); SFormat = sFormat. toUpperCase (); If (sFormat. search (/MMMM | MMM/) + 1) {// replace Mar/March with 03, etc. SDate = sDate. replace (new RegExp ('+ ShortMths. join (' | ') +') [A-Z] * ', 'gi '), Function (m ){ Var I = ShortMths. indexOf (m. charAt (0). toUpperCase () + M. substr (1, 2). toLowerCase () + 1; Return (I <10 )? "0" + I: "" + I). toString (); }); SFormat = sFormat. replace (/MMMM | MMM/g, 'mm '); } If (sFormat. search (/DDDD | DDD/) + 1) {// replace Tue/Tuesday, etc.'' SDate = sDate. replace (new RegExp ('+ lifecycle days. join (' | ') +') [A-Z] * ', 'gi '),''); SFormat = sFormat. replace (/DDDD | DDD/g ,''); } SDate = sDate. replace (/(^ | \ D) (\ d )(? = \ D | $)/g, function ($0, $1, $2 ){ // Single digits 2 with 02 Return $1 + '0' + $2; }); SFormat = sFormat. replace (/(^ | [^ DMY]) (D | M )(? = [^ DMY] | $)/g, function ($0, $1, $2 ){ Return $1 + $2 + $2; // replace D or M with DD and MM }); // Are there still single Ds or MS? FndSingle = sFormat. search (/(^ | [^ D]) D ([^ D] | $) | (^ | [^ M]) M ([^ M] | $ )/) + 1; // Do not attempt to parse, for example, 'dmm' If (fndSingle) return ''; SFormat = sFormat. replace (/(^ | [^ Y]) (YY )(? = [^ Y] | $)/g, function ($0, $1, $2, index ){ Var tempDate = sDate. substr (0, index + 1 ); TempDate + = (cutOff )? (ParseInt (sDate. substr (index + 1, 2), 10)> cutOff )? '19': '20'): '20 '; TempDate + = sDate. substr (index + 1 ); SDate = tempDate; Return $1 + $2 + $2; }); SParsed = ('yyyy/MM/dd'). replace (/YYYY | MM | DD/g, function (m ){ Return (sFormat. indexOf (m) + 1 )? SDate. substr (sFormat. indexOf (m), m. length ):''; }); If (sParsed. charAt (0) = '/'){ // If no year specified, assume the current year SParsed = (new Date (). getFullYear () + sParsed; } If (sParsed. charAt (sParsed. length-1) = '/'){ // If no date, assume the 1st of the month SParsed + = '01 '; } // Shocould end up with 10 characters .. Return (sParsed. length = 10 )? SParsed :''; }; |