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 相关文章推荐
探索Emberjs制作一个简单的Todo应用
Nov 07 Javascript
node.js中的console.info方法使用说明
Dec 09 Javascript
JavaScript动态添加列的方法
Mar 25 Javascript
究竟什么是Node.js?Node.js有什么好处?
May 29 Javascript
JavaScript函数节流的两种写法
Apr 07 Javascript
深入探究AngularJs之$scope对象(作用域)
Jul 20 Javascript
详解vue-router 路由元信息
Sep 13 Javascript
基于Vue的SPA动态修改页面title的方法(推荐)
Jan 02 Javascript
JS正则表达式常见用法实例详解
Jun 19 Javascript
基于D3.js实现时钟效果
Jul 17 Javascript
Vue 通过公共字段,拼接两个对象数组的实例
Nov 07 Javascript
详解jQuery中的prop()使用方法
Jan 05 jQuery
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 CURL获取邮箱地址的详解
2013/06/03 PHP
深入解析PHP的引用计数机制
2013/06/14 PHP
php.ini 配置文件的深入解析
2013/06/17 PHP
PHP反向代理类代码
2014/08/15 PHP
php统计数组元素个数的方法
2015/07/02 PHP
PHP各种常见经典算法总结【排序、查找、翻转等】
2019/08/05 PHP
js中arguments,caller,callee,apply的用法小结
2014/01/28 Javascript
JQuery设置获取下拉菜单某个选项的值(比较全)
2014/08/05 Javascript
JS实现具备延时功能的滑动门菜单效果
2015/09/17 Javascript
三分钟带你玩转jQuery.noConflict()
2016/02/15 Javascript
javascript实现鼠标点击页面 移动DIV
2016/12/02 Javascript
详解vue-cli开发环境跨域问题解决方案
2017/06/06 Javascript
js精确的加减乘除实例
2017/11/14 Javascript
angular6.0开发教程之如何安装angular6.0框架
2018/06/29 Javascript
AngularJS修改model值时,显示内容不变的实例
2018/09/13 Javascript
Jquery实现无缝向上循环滚动列表的特效
2019/02/13 jQuery
Vue路由守卫之路由独享守卫
2019/09/25 Javascript
vue实现购物车的监听
2020/04/20 Javascript
Python开发编码规范
2006/09/08 Python
基于Python实现文件大小输出
2016/01/11 Python
Python常见的pandas用法demo示例
2019/03/16 Python
jupyter 中文乱码设置编码格式 避免控制台输出的解决
2020/04/20 Python
Spring http服务远程调用实现过程解析
2020/06/11 Python
Python用来做Web开发的优势有哪些
2020/08/05 Python
Python在后台自动解压各种压缩文件的实现方法
2020/11/10 Python
Surfdome西班牙:世界上最受欢迎的生活方式品牌
2019/02/13 全球购物
纯净、自信、100%的羊绒服装:360Cashmere
2021/02/20 全球购物
幼儿教师考核制度
2014/01/25 职场文书
竞争上岗实施方案
2014/03/21 职场文书
《乡下孩子》教学反思
2014/04/17 职场文书
法人授权委托书样本
2014/09/19 职场文书
2014年宣传部个人工作总结
2014/12/06 职场文书
企业管理制度设计时要注意的几种“常见病”!
2019/04/19 职场文书
原来闭幕词是这样写的呀!
2019/07/01 职场文书
Windows下使用Nginx+Tomcat做负载均衡的完整步骤
2021/03/31 Servers
关于flex 上下文中自动 margin的问题(完整例子)
2021/05/20 HTML / CSS