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下判断一个元素是否存在的代码
Mar 05 Javascript
javaScript同意等待代码实现心得
Jan 01 Javascript
奉献给JavaScript初学者的编写开发的七个细节
Jan 11 Javascript
javascript引用赋值(地址传值)用法实例
Jan 13 Javascript
深入理解JavaScript 函数
Jun 06 Javascript
JavaScript提高网站性能优化的建议(二)
Jul 24 Javascript
基于vue组件实现猜数字游戏
May 28 Javascript
vue的安装及element组件的安装方法
Mar 09 Javascript
微信小程序冒泡事件及其阻止方法实例分析
Dec 06 Javascript
Vue插槽原理与用法详解
Mar 05 Javascript
详解用场景去理解函数柯里化(入门篇)
Apr 11 Javascript
vue.js中使用微信扫一扫解决invalid signature问题(完美解决)
Apr 11 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
PHP中查询SQL Server或Sybase时TEXT字段被截断的解决方法
2009/03/10 PHP
PHP大小写问题:函数名和类名不区分,变量名区分
2013/06/17 PHP
php中的Base62类(适用于数值转字符串)
2013/08/12 PHP
php中照片旋转 (orientation) 问题的正确处理
2017/02/16 PHP
php5.5使用PHPMailer-5.2发送邮件的完整步骤
2018/10/14 PHP
laravel 解决多库下的DB::transaction()事务失效问题
2019/10/21 PHP
document.open() 与 document.write()的区别
2007/08/13 Javascript
常用简易JavaScript函数
2009/04/09 Javascript
用 Javascript 验证表单(form)中的单选(radio)值
2009/09/08 Javascript
33种Javascript 表格排序控件收集
2009/12/03 Javascript
jquery中checkbox全选失效的解决方法
2014/12/26 Javascript
html判断当前页面是否在iframe中的实例
2016/11/30 Javascript
利用JS轻松实现获取表单数据
2016/12/06 Javascript
canvas实现粒子时钟效果
2017/02/06 Javascript
基于JavaScript实现全选、不选和反选效果
2017/02/15 Javascript
JavaScript高阶函数_动力节点Java学院整理
2017/06/28 Javascript
JavaScript数据结构之双向链表定义与使用方法示例
2017/10/27 Javascript
浅谈webpack打包过程中因为图片的路径导致的问题
2018/02/21 Javascript
Typescript3.9 常用新特性一览(推荐)
2020/05/14 Javascript
vue+element实现图片上传及裁剪功能
2020/06/29 Javascript
JavaScript中CreateTextFile函数
2020/08/30 Javascript
JavaScript实现网页跨年倒计时
2020/12/02 Javascript
python实现用于测试网站访问速率的方法
2015/05/26 Python
Python使用中文正则表达式匹配指定中文字符串的方法示例
2017/01/20 Python
Python之自动获取公网IP的实例讲解
2017/10/01 Python
python使用tkinter实现简单计算器
2018/01/30 Python
10分钟用python搭建一个超好用的CMDB系统
2019/07/17 Python
Python中的延迟绑定原理详解
2019/10/11 Python
使用Pytorch来拟合函数方式
2020/01/14 Python
PyCharm 2020 激活到 2100 年的教程
2020/03/25 Python
Django项目创建及管理实现流程详解
2020/10/13 Python
深入浅析css3 border-image边框图像详解
2015/11/24 HTML / CSS
举例说明类变量和实例变量的区别
2016/06/30 面试题
防灾减灾标语
2014/10/07 职场文书
2015高考寄语集锦
2015/02/27 职场文书