国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript通過Date-Mask將日期轉換成字符串的方法

2019-11-20 12:21:42
字體:
來源:轉載
供稿:網友

本文實例講述了JavaScript通過Date-Mask將日期轉換成字符串的方法。分享給大家供大家參考。具體實現方法如下:

var MonthNames = ["January", "February", "March", "April", "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 ShortDays = ["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. with ''    sDate = sDate.replace(new RegExp('(' + ShortDays.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';  }  // should end up with 10 characters..  return ( sParsed.length == 10 ) ? sParsed : '';};

希望本文所述對大家的javascript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长岛县| 五原县| 中山市| 大安市| 金门县| 建阳市| 迁安市| 乐都县| 文登市| 靖安县| 霸州市| 延川县| 凤山市| 磐石市| 隆安县| 大化| 文成县| 老河口市| 旬邑县| 吉木乃县| 伊吾县| 波密县| 新宁县| 锦州市| 浙江省| 南岸区| 阳原县| 宜宾县| 婺源县| 合山市| 新乐市| 临汾市| 海阳市| 宁德市| 兴文县| 桦甸市| 龙口市| 阳西县| 东乡| 专栏| 金湖县|