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使用中为什么10..toString()正常而10.toString()出错呢
Jan 11 Javascript
JS替换文本域内的回车示例
Feb 18 Javascript
jquery提示效果实例分析
Nov 25 Javascript
JavaScript优化专题之Loading and Execution加载和运行
Jan 20 Javascript
js鼠标按键事件和键盘按键事件用法实例汇总
Oct 03 Javascript
jquery Ajax 全局调用封装实例详解
Jan 16 Javascript
js实现消息滚动效果
Jan 18 Javascript
jquery对table做排序操作的实例演示
Aug 10 jQuery
Vue.js结合bootstrap前端实现分页和排序效果
Dec 29 Javascript
nuxt.js添加环境变量,区分项目打包环境操作
Nov 06 Javascript
vue实现顶部菜单栏
Nov 08 Javascript
利用javaScript处理常用事件详解
Apr 14 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的面向对象编程
2006/10/09 PHP
BBS(php &amp; mysql)完整版(六)
2006/10/09 PHP
用session做客户验证时的注意事项
2006/10/09 PHP
PHP+MySQL5.0中文乱码解决方法
2006/11/20 PHP
最准确的php截取字符串长度函数
2015/10/29 PHP
YII Framework框架教程之缓存用法详解
2016/03/14 PHP
在Laravel框架里实现发送邮件实例(邮箱验证)
2016/05/20 PHP
利用PHPStorm如何开发Laravel应用详解
2017/08/30 PHP
使用PHPExcel导出Excel表
2018/09/08 PHP
PHP实现无限极分类的两种方式示例【递归和引用方式】
2019/03/25 PHP
关于ExtJS4.1:快捷键支持的问题
2013/04/24 Javascript
JavaScript计算字符串中每个字符出现次数的小例子
2013/07/02 Javascript
详解JavaScript中的事件流和事件处理程序
2016/05/20 Javascript
Js得到radiobuttonlist选中值的两种方法(推荐)
2016/08/25 Javascript
Bootstrap基本样式学习笔记之标签(5)
2016/12/07 Javascript
select自定义小三角样式代码(实用总结)
2017/08/18 Javascript
微信小程序实现图片预览功能
2018/01/31 Javascript
Vue 页面切换效果之 BubbleTransition(推荐)
2018/04/08 Javascript
解决vue 打包发布去#和页面空白的问题
2018/09/04 Javascript
浅探express路由和中间件的实现
2019/09/30 Javascript
vue+koa2搭建mock数据环境的详细教程
2020/05/18 Javascript
axios解决高并发的方法:axios.all()与axios.spread()的操作
2020/11/09 Javascript
[02:41]2015国际邀请赛中国区预选赛观战指南
2015/05/20 DOTA
使用Python的判断语句模拟三目运算
2015/04/24 Python
Python利用multiprocessing实现最简单的分布式作业调度系统实例
2017/11/14 Python
Python实现转换图片背景颜色代码
2020/04/30 Python
推荐10个HTML5响应式框架
2016/02/25 HTML / CSS
HTML5和CSS3实例教程总结(推荐)
2016/07/18 HTML / CSS
JRE、JDK、JVM之间的关系怎样
2012/05/16 面试题
《三顾茅庐》教学反思
2014/04/10 职场文书
社区娱乐活动方案
2014/08/21 职场文书
租房协议书
2014/09/12 职场文书
大学生求职自荐信
2015/03/24 职场文书
MySQL InnoDB ReplicaSet(副本集)简单介绍
2021/04/24 MySQL
Pandas 数据编码的十种方法
2022/04/20 Python
详解CSS中postion和opacity及cursor的特性
2022/08/14 HTML / CSS