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中input中readonly和disabled区别介绍
Oct 23 Javascript
js为数字添加逗号并格式化数字的代码
Aug 23 Javascript
node.js中的querystring.escape方法使用说明
Dec 10 Javascript
js实现跨域的4种实用方法原理分析
Oct 29 Javascript
延时加载JavaScript代码提高速度
Dec 27 Javascript
JS实现动态增加和删除li标签行的实例代码
Oct 16 Javascript
JavaScript计算值然后把值嵌入到html中的实现方法
Oct 29 Javascript
Bootstrap fileinput组件封装及使用详解
Mar 10 Javascript
vue基于Vue2.0和高德地图的地图组件实例
Apr 28 Javascript
浅析Angular19 自定义表单控件
Jan 31 Javascript
vue实现一拉到底的滑动验证
Jul 25 Javascript
解决vue更新路由router-view复用组件内容不刷新的问题
Nov 04 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开发需要注意的安全问题
2010/09/01 PHP
PHP语法小结之基础和变量
2015/11/22 PHP
PHP设计模式之简单投诉页面实例
2016/02/24 PHP
Thinkphp自定义代码生成工具及用法说明(附下载地址)
2016/05/27 PHP
zend框架实现支持sql server的操作方法
2016/12/08 PHP
PHP实现简单的计算器
2020/08/28 PHP
基于Jquery的简单&amp;简陋Tabs插件代码
2010/02/09 Javascript
javascript获取select的当前值示例代码(兼容IE/Firefox/Opera/Chrome)
2013/12/17 Javascript
jquery列表拖动排列(由项目提取相当好用)
2014/06/17 Javascript
javascript实现图片自动和可控的轮播切换特效
2015/04/13 Javascript
jQuery fadeOut 异步实例代码详解
2016/08/18 Javascript
关于vue-router路径计算问题
2017/05/10 Javascript
详解webpack3编译兼容IE8的正确姿势
2017/12/21 Javascript
浅谈Angular7 项目开发总结
2018/12/19 Javascript
Vue+abp微信扫码登录的实现代码示例
2020/01/06 Javascript
[45:15]Optic vs VP 2018国际邀请赛淘汰赛BO3 第一场 8.24
2018/08/25 DOTA
使用python实现正则匹配检索远端FTP目录下的文件
2015/03/25 Python
Python优化技巧之利用ctypes提高执行速度
2016/09/11 Python
Python实现对文件进行单词划分并去重排序操作示例
2018/07/10 Python
linux查找当前python解释器的位置方法
2019/02/20 Python
Python面向对象思想与应用入门教程【类与对象】
2019/04/12 Python
python使用BeautifulSoup与正则表达式爬取时光网不同地区top100电影并对比
2019/04/15 Python
Django框架组成结构、基本概念与文件功能分析
2019/07/30 Python
Python基于BeautifulSoup和requests实现的爬虫功能示例
2019/08/02 Python
Jupyter notebook运行Spark+Scala教程
2020/04/10 Python
多个版本的python共存时使用pip的正确做法
2020/10/26 Python
python实现PolynomialFeatures多项式的方法
2021/01/06 Python
Django url 路由匹配过程详解
2021/01/22 Python
使用HTML5和CSS3表单验证功能
2017/05/05 HTML / CSS
大学生应聘自荐信
2013/10/11 职场文书
前处理组长岗位职责
2014/03/01 职场文书
教师先进事迹材料
2014/12/16 职场文书
2015年党员个人剖析材料
2014/12/18 职场文书
档案管理员岗位职责
2015/02/12 职场文书
外贸业务员岗位职责
2015/02/13 职场文书
纯CSS实现酷炫的霓虹灯效果
2021/04/13 HTML / CSS