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


Posted in Javascript onJune 04, 2015

本文实例讲述了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 相关文章推荐
JavaScript 继承的实现
Jul 09 Javascript
javascript针对DOM的应用分析(二)
Apr 15 Javascript
javascript ie6兼容position:fixed实现思路
Apr 01 Javascript
js常用系统函数用法实例分析
Jan 12 Javascript
JS实现仿google、百度搜索框输入信息智能提示的实现方法
Apr 20 Javascript
JS实现的鼠标跟随代码(卡通手型点击效果)
Oct 26 Javascript
JS实现的仿QQ空间图片弹出效果代码
Feb 23 Javascript
解决vue2中使用axios http请求出现的问题
Mar 05 Javascript
详解Angular如何正确的操作DOM
Jul 06 Javascript
解决angular2在双向数据绑定时[(ngModel)]无法使用的问题
Sep 13 Javascript
Vue中实现权限控制的方法示例
Jun 07 Javascript
Vue组件模板及组件互相引用代码实例
Mar 11 Javascript
JavaScript中Number.MIN_VALUE属性的使用示例
Jun 04 #Javascript
JavaScript中Number.MAX_VALUE属性的使用方法
Jun 04 #Javascript
深入理解JavaScript中的对象
Jun 04 #Javascript
详解JavaScript中void语句的使用
Jun 04 #Javascript
用JavaScript实现对话框的教程
Jun 04 #Javascript
用JavaScript实现页面重定向功能的教程
Jun 04 #Javascript
javascript原型模式用法实例详解
Jun 04 #Javascript
You might like
使用php判断服务器是否支持Gzip压缩功能
2013/09/24 PHP
php文件上传的例子及参数详解
2013/12/12 PHP
PHP判断是否有Get参数的方法
2014/05/05 PHP
smarty模板引擎基础知识入门
2015/03/30 PHP
PHP的运行机制与原理(底层)
2015/11/16 PHP
PHP编程中尝试程序并发的几种方式总结
2016/03/21 PHP
PHP校验15位和18位身份证号的类封装
2018/11/07 PHP
Laravel 解决composer相关操作提示php相关异常的问题
2019/10/23 PHP
php实现的简单多进程服务器类完整示例
2020/02/01 PHP
tp5.1 框架路由操作-URL生成实例分析
2020/05/26 PHP
兼容ie和firefox js关闭代码
2008/12/11 Javascript
javascript操作cookie_获取与修改代码
2009/05/21 Javascript
Jquery 点击按钮显示和隐藏层的代码
2011/07/25 Javascript
jQuery弹出框代码封装DialogHelper
2015/01/30 Javascript
jquery插件star-rating.js实现星级评分特效
2015/04/15 Javascript
功能强大的Bootstrap组件(结合js)
2016/08/03 Javascript
微信小程序 高德地图SDK详解及简单实例(源码下载)
2017/01/11 Javascript
浅析javaScript中的浅拷贝和深拷贝
2017/02/15 Javascript
angular.js 路由及页面传参示例
2017/02/24 Javascript
详解Angular 自定义结构指令
2017/06/21 Javascript
jQuery中ajax获取数据赋值给页面的实例
2017/12/31 jQuery
vue 实现全选全不选的示例代码
2018/03/29 Javascript
JavaScript实现移动小精灵的案例代码
2020/12/12 Javascript
python僵尸进程产生的原因
2017/07/21 Python
Python可视化mhd格式和raw格式的医学图像并保存的方法
2019/01/24 Python
python Manager 之dict KeyError问题的解决
2019/12/21 Python
python实现126邮箱发送邮件
2020/05/20 Python
matplotlib.pyplot.matshow 矩阵可视化实例
2020/06/16 Python
python根据用户需求输入想爬取的内容及页数爬取图片方法详解
2020/08/03 Python
使用css3绘制出各种几何图形
2016/08/17 HTML / CSS
美国廉价机票预订网站:Cheapfaremart
2018/04/28 全球购物
个人实用的自我评价范文
2013/11/23 职场文书
上学迟到的检讨书
2014/01/11 职场文书
《守株待兔》教学反思
2014/03/01 职场文书
幼儿园毕业寄语
2014/04/03 职场文书
经理岗位职责
2015/02/02 职场文书