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 图片预加载 自动等比例缩放插件
Dec 25 Javascript
firefox下jQuery UI Autocomplete 1.8.*中文输入修正方法
Sep 19 Javascript
关闭ie窗口清除Session的解决方法
Jan 10 Javascript
jQuery中nextUntil()方法用法实例
Jan 07 Javascript
基于canvas实现的钟摆效果完整实例
Jan 26 Javascript
jqGrid用法汇总(全经典)
Jun 28 Javascript
angularjs 源码解析之scope
Aug 22 Javascript
js实现音频控制进度条功能
Apr 01 Javascript
JQuery EasyUI 结合ztrIee的后台页面开发实例
Sep 01 jQuery
详解Node.js模板引擎Jade入门
Jan 19 Javascript
分享vue里swiper的一些坑
Aug 30 Javascript
js html实现计算器功能
Nov 13 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
fleaphp crud操作之find函数的使用方法
2011/04/23 PHP
PHP 文件编程综合案例-文件上传的实现
2013/07/03 PHP
开启PHP Static 关键字之旅模式
2015/11/13 PHP
PHP实现实时生成并下载超大数据量的EXCEL文件详解
2017/10/23 PHP
学习YUI.Ext第五日--做拖放Darg&amp;Drop
2007/03/10 Javascript
无刷新预览所选择的图片示例代码
2014/04/02 Javascript
php的文件上传入门教程(实例讲解)
2014/04/10 Javascript
js中的for如何实现foreach中的遍历
2014/05/31 Javascript
JavaScript使用yield模拟多线程的方法
2015/03/19 Javascript
jQuery+PHP星级评分实现方法
2015/10/02 Javascript
JavaScript实现的多种鼠标拖放效果
2015/11/03 Javascript
js判断当前页面用什么浏览器打开的方法
2016/01/06 Javascript
AngularJS基础 ng-readonly 指令简单示例
2016/08/02 Javascript
JavaScript中递归实现的方法及其区别
2017/09/12 Javascript
javaScript实现复选框全选反选事件详解
2020/11/20 Javascript
jQuery利用cookie 实现本地收藏功能(不重复无需多次命名)
2019/11/07 jQuery
React学习之JSX与react事件实例分析
2020/01/06 Javascript
vue计算属性+vue中class与style绑定(推荐)
2020/03/30 Javascript
vue项目中自定义video视频控制条的实现代码
2020/04/26 Javascript
Vue实现手机计算器
2020/08/17 Javascript
使用Python生成url短链接的方法
2015/05/04 Python
利用TensorFlow训练简单的二分类神经网络模型的方法
2018/03/05 Python
python打包压缩、读取指定目录下的指定类型文件
2018/04/12 Python
详解python websocket获取实时数据的几种常见链接方式
2019/07/01 Python
Python中的引用和拷贝实例解析
2019/11/14 Python
Python代码生成视频的缩略图的实例讲解
2019/12/22 Python
python保存log日志,实现用log日志画图
2019/12/24 Python
ipython jupyter notebook中显示图像和数学公式实例
2020/04/15 Python
基于Python爬取搜狐证券股票过程解析
2020/11/18 Python
CSS3样式linear-gradient的使用实例
2017/01/16 HTML / CSS
安全员岗位职责
2013/11/11 职场文书
作文评语怎么写
2014/12/25 职场文书
给客户的感谢信
2015/01/21 职场文书
员工考勤管理制度
2015/08/06 职场文书
个人业务学习心得体会
2016/01/25 职场文书
windows server 2012安装FTP并配置被动模式指定开放端口
2022/06/10 Servers