JavaScript converts dates to strings through date mask

From , 5 Years ago, written in JavaScript, viewed 70 times.
URL https://pastebin.vip/view/7b99efbc
  1. var MonthNames = ["January", "February", "March", "April", "May", "June", "July",
  2.     "August", "September", "October", "November", "December"];
  3. var DayNames = [ "Sunday", "Monday", "Tueday", "Wednesday", "Thursday",
  4.     "Friday", "Saturday" ];
  5. var ShortMths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
  6.     "Sep", "Oct", "Nov", "Dec"];
  7. var ShortDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  8.  
  9. var StringToDate = function (sDate, sFormat, cutOff) {
  10.     // Input: a date value as a string, it's format as a string e.g. 'dd-mmm-yy'
  11.     // Optional: a cutoff (integer) for 2 digit years.
  12.     // If no 'd' appears in the format string then the 1st of the month is assumed.
  13.     // If the year is 20 and the cut-off is 30 then the value will be converted
  14.     // to 2020; if the year is 40 then this will be converted to 1940.
  15.     // If no cut-off is supplied then '20' will be pre-pended to the year (YY).
  16.     // Output: a string in the format 'YYYY/MM/DD' or ''
  17.     // Will not attempt to convert certain combinations e.g. DMM, MDD, DDM, YYYYD.
  18.     // http://andrew.dx.am
  19.     var sParsed, fndSingle;
  20.     // sParsed will be constructed in the format 'YYYY/MM/DD'
  21.     sDate = sDate.toString().toUpperCase();
  22.     sFormat = sFormat.toUpperCase();
  23.    
  24.     if (sFormat.search(/MMMM|MMM/) + 1) {       // replace Mar/March with 03, etc.
  25.         sDate = sDate.replace(new RegExp('(' + ShortMths.join('|') + ')[A-Z]*', 'gi'),
  26.             function (m) {
  27.             var i = ShortMths.indexOf(m.charAt(0).toUpperCase() +
  28.                 m.substr(1, 2).toLowerCase()) + 1;
  29.             return ((i < 10) ? "0" + i : "" + i).toString();
  30.         });
  31.         sFormat = sFormat.replace(/MMMM|MMM/g, 'MM');
  32.     }
  33.     if (sFormat.search(/DDDD|DDD/) + 1) {       // replace Tue/Tuesday, etc. with ''
  34.         sDate = sDate.replace(new RegExp('(' + ShortDays.join('|') + ')[A-Z]*', 'gi'),'');
  35.         sFormat = sFormat.replace(/DDDD|DDD/g, '');
  36.     }
  37.     sDate = sDate.replace(/(^|\D)(\d)(?=\D|$)/g, function($0, $1, $2) {
  38.         // single digits 2 with 02
  39.         return $1 + '0' + $2;
  40.     });
  41.     sFormat = sFormat.replace(/(^|[^DMY])(D|M)(?=[^DMY]|$)/g, function($0, $1, $2){
  42.         return $1 + $2 + $2;        // replace D or M with DD and MM
  43.     });
  44.     // are there still single Ds or Ms?
  45.     fndSingle = sFormat.search(/(^|[^D])D([^D]|$)|(^|[^M])M([^M]|$)/)+1;
  46.     // do not attempt to parse, for example, 'DMM'
  47.     if ( fndSingle ) return '';
  48.     sFormat = sFormat.replace(/(^|[^Y])(YY)(?=[^Y]|$)/g, function($0, $1, $2, index) {
  49.         var tempDate = sDate.substr(0, index + 1);
  50.         tempDate += (cutOff) ? ((parseInt(sDate.substr(index + 1, 2),10) > cutOff) ?
  51.             '19' : '20') : '20';
  52.         tempDate += sDate.substr(index + 1);
  53.         sDate = tempDate;
  54.         return $1 + $2 + $2;
  55.     });
  56.     sParsed = ('YYYY/MM/DD').replace(/YYYY|MM|DD/g, function(m){
  57.         return (sFormat.indexOf(m) + 1) ?
  58.             sDate.substr(sFormat.indexOf(m), m.length) : '';
  59.     });
  60.     if (sParsed.charAt(0) == '/') {
  61.         // if no year specified, assume the current year
  62.         sParsed = (new Date().getFullYear()) + sParsed;
  63.     }
  64.     if (sParsed.charAt(sParsed.length - 1) == '/') {
  65.         // if no date, assume the 1st of the month
  66.         sParsed += '01';
  67.     }
  68.     // should end up with 10 characters..
  69.     return ( sParsed.length == 10 ) ? sParsed : '';
  70. };
  71. //javascript/5213

Reply to "JavaScript converts dates to strings through date mask"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website