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 相关文章推荐
jQuery Autocomplete自动完成插件
Jul 17 Javascript
纯CSS打造的导航菜单(附jquery版)
Aug 07 Javascript
IE中jquery.form中ajax提交没反应解决方法分享
Sep 11 Javascript
浅谈javascript的分号的使用
May 12 Javascript
JQuery中Text方法用法实例分析
May 18 Javascript
js 将图片连接转换成base64格式的简单实例
Aug 10 Javascript
郁闷!ionic中获取ng-model绑定的值为undefined如何解决
Aug 27 Javascript
jQuery替换节点用法示例(使用replaceWith方法)
Sep 08 Javascript
AngularJS路由实现页面跳转实例
Mar 03 Javascript
详解基于electron制作一个node压缩图片的桌面应用
Jan 29 Javascript
使用Vue父子组件通信实现todolist的功能示例代码
Apr 11 Javascript
JavaScript自动生成 年月范围 选择功能完整示例【基于jQuery插件】
Sep 03 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
GBK的页面输出JSON格式的php函数
2010/02/16 PHP
PHP文件打开、关闭、写入的判断与执行代码
2011/05/24 PHP
解析PHP工厂模式的好处
2013/06/18 PHP
yii2实现根据时间搜索的方法
2016/05/25 PHP
Zend Framework数据库操作技巧总结
2017/02/18 PHP
PHP使用preg_split和explode分割textarea存放内容的方法分析
2017/07/03 PHP
PHP实现从PostgreSQL数据库检索数据分页显示及根据条件查找数据示例
2018/06/09 PHP
laravel框架语言包拓展实现方法分析
2019/11/22 PHP
javascript 动态加载 css 方法总结
2009/07/11 Javascript
js 有框架页面跳转(target)三种情况下的应用
2013/04/09 Javascript
检查输入的是否是数字使用keyCode配合onkeypress事件
2014/01/23 Javascript
JavaScript日期时间格式化函数分享
2014/05/05 Javascript
JavaScript字符串对象replace方法实例(用于字符串替换或正则替换)
2014/10/16 Javascript
什么是 AngularJS?AngularJS简介
2014/12/06 Javascript
JS中多步骤多分步的StepJump组件实例详解
2016/04/01 Javascript
JavaScript中双符号的运算详解
2017/03/12 Javascript
BootStrap+Mybatis框架下实现表单提交数据重复验证
2017/03/23 Javascript
详解Node.js开发中的express-session
2017/05/19 Javascript
Angular限制input框输入金额(是小数的话只保留两位小数点)
2017/07/13 Javascript
JavaScript轮播停留效果的实现思路
2018/05/24 Javascript
小程序接入腾讯位置服务的详细流程
2020/03/03 Javascript
在react项目中使用antd的form组件,动态设置input框的值
2020/10/24 Javascript
Python函数式编程指南(一):函数式编程概述
2015/06/24 Python
python实现下载整个ftp目录的方法
2017/01/17 Python
Python语言生成水仙花数代码示例
2017/12/18 Python
对python中Json与object转化的方法详解
2018/12/31 Python
python遍历小写英文字母的方法
2019/01/02 Python
ubuntu 18.04搭建python环境(pycharm+anaconda)
2019/06/14 Python
python 字典访问的三种方法小结
2019/12/05 Python
利用python实现后端写网页(flask框架)
2021/02/28 Python
详解使用canvas保存网页为pdf文件支持跨域
2018/11/23 HTML / CSS
经典安踏广告词
2014/03/21 职场文书
活动宣传策划方案
2014/05/23 职场文书
证婚人致辞精选
2015/07/28 职场文书
清洁工工作总结
2015/08/11 职场文书
再读《皇帝的新衣》的读后感悟!
2019/08/07 职场文书