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 相关文章推荐
js 对象是否存在判断
Jul 15 Javascript
基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码(带平滑移动的效果)
May 24 Javascript
jQuery插件 selectToSelect使用方法
Oct 02 Javascript
javascript创建数组之联合数组的使用方法示例
Dec 26 Javascript
Jquery利用mouseenter和mouseleave实现鼠标经过弹出层且可以点击
Feb 12 Javascript
js改变embed标签src值的方法
Apr 10 Javascript
JS+CSS实现美化的下拉列表框效果
Aug 11 Javascript
jquery 实现回车登录详解及实例代码
Oct 23 Javascript
原生ajax处理json格式数据的实例代码
Dec 25 Javascript
react.js 翻页插件实例代码
Jan 19 Javascript
详解基于vue-cli3.0如何构建功能完善的前端架子
Oct 09 Javascript
微信小程序云开发实现数据添加、查询和分页
May 17 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和XSS跨站攻击的防范
2007/04/17 PHP
php+ajax导入大数据时产生的问题处理
2014/06/11 PHP
PHP设计模式之工厂模式与单例模式
2016/09/28 PHP
php文件后缀不强制为.php的实操方法
2019/09/18 PHP
javascript中的location用法简单介绍
2007/03/07 Javascript
获取HTML DOM节点元素的方法的总结
2009/08/21 Javascript
JavaScript 一行代码,轻松搞定浮动快捷留言-V2升级版
2010/04/02 Javascript
javascript 日期时间 转换的方法
2013/02/21 Javascript
jQuery瀑布流插件Wookmark使用实例
2014/04/02 Javascript
js获取 type=radio 值的方法
2014/05/09 Javascript
js动态切换图片的方法
2015/01/20 Javascript
javascript中typeof操作符和constucor属性检测
2015/02/26 Javascript
jquery任意位置浮动固定层插件用法实例
2015/05/29 Javascript
详解JavaScript中shift()方法的使用
2015/06/09 Javascript
Js制作点击输入框时默认文字消失的效果
2015/09/05 Javascript
zTree插件下拉树使用入门教程
2016/04/11 Javascript
jQuery模拟窗口抖动效果
2017/03/15 Javascript
js实现鼠标拖动功能
2017/03/20 Javascript
原生JS与jQuery编写简单选项卡
2017/10/30 jQuery
vue data恢复初始化数据的实现方法
2019/10/31 Javascript
vue中axios防止多次触发终止多次请求的示例代码(防抖)
2020/02/16 Javascript
Vue实现菜单切换功能
2020/11/08 Javascript
使用webpack和rollup打包组件库的方法
2021/02/25 Javascript
Python安装lz4-0.10.1遇到的坑
2018/05/20 Python
在pycharm 中添加运行参数的操作方法
2019/01/19 Python
pytorch加载自定义网络权重的实现
2020/01/07 Python
Selenium常见异常解析及解决方案示范
2020/04/10 Python
Python Excel vlookup函数实现过程解析
2020/06/22 Python
一些Unix笔试题和面试题
2012/09/25 面试题
Python如何实现单例模式
2016/06/03 面试题
出纳岗位职责模板
2013/11/27 职场文书
运动会拉拉队口号
2014/06/09 职场文书
审美与表现自我评价
2015/03/09 职场文书
装饰技术负责人岗位职责
2015/04/13 职场文书
地心历险记观后感
2015/06/15 职场文书
redis不能访问本机真实ip地址的解决方案
2021/07/07 Redis