JavaScript通过Date-Mask将日期转换成字符串的方法

 更新时间:2015年06月04日 17:08:51   作者:不吃皮蛋  
这篇文章主要介绍了JavaScript通过Date-Mask将日期转换成字符串的方法,涉及javascript日期、数组及字符串操作的相关技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • 原生javascript和jquery判断浏览器版本等信息

    原生javascript和jquery判断浏览器版本等信息

    本文为大家详细介绍下通过jquery和原生javascript判断浏览器信息包括:判断浏览器是否为IE以及IE版本是多少等等,感兴趣的朋友可以参考下哈,希望对大家有所帮助
    2013-07-07
  • JS实现转动随机数抽奖特效代码

    JS实现转动随机数抽奖特效代码

    这篇文章主要为大家详细介绍了一款转动随机数抽奖的JS特效代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2015-08-08
  • javascript实现详细时间提醒信息效果的方法

    javascript实现详细时间提醒信息效果的方法

    这篇文章主要介绍了javascript实现详细时间提醒信息效果的方法,涉及javascript操作时间的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • javascript实现无缝上下滚动特效

    javascript实现无缝上下滚动特效

    这篇文章主要介绍了javascript实现无缝上下滚动特效的相关资料,需要的朋友可以参考下
    2015-12-12
  • js实现图片推拉门效果代码实例

    js实现图片推拉门效果代码实例

    这篇文章主要介绍了js实现图片推拉门效果代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • JavaScript学习小结(一)——JavaScript入门基础

    JavaScript学习小结(一)——JavaScript入门基础

    本教程比较适合javascript初学者,对javascript基本知识的小结包括变量,基本类型等知识点,需要的朋友一起来学习吧
    2015-09-09
  • 网站内容禁止复制和粘贴、另存为的js代码

    网站内容禁止复制和粘贴、另存为的js代码

    这篇文章主要介绍了JS如何实现网站内容如何实现禁止复制和粘贴、另存为,需要的朋友可以参考下
    2014-02-02
  • JS解密入门之凭直觉解

    JS解密入门之凭直觉解

    什么叫直觉解?根据目前流行的免杀方法,有这么几种初级加密方法。进制加密,URLEncode加密,加花
    2008-06-06
  • JavaScript改变函数作用域的方法示例

    JavaScript改变函数作用域的方法示例

    本文主要介绍了JavaScript改变函数作用域的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • webpack proxy如何解决跨域问题

    webpack proxy如何解决跨域问题

    webpack proxy,即webpack提供的代理服务,基本行为就是接收客户端发送的请求后转发给其他服务器,本文给大家介绍了webpack proxy如何解决跨域问题,感兴趣的朋友跟随小编一起看看吧
    2023-10-10

最新评论