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 相关文章推荐
把JS与CSS写在同一个文件里的书写方法
Jun 02 Javascript
使用JavaScript库还是自己写代码?
Jan 28 Javascript
js 判断计算字符串长度/判断空的简单方法
Aug 05 Javascript
用javascript替换URL中的参数值示例代码
Jan 27 Javascript
JavaScript动态添加列的方法
Mar 25 Javascript
jQuery+ajax简单实现文件上传的方法
Jun 03 Javascript
完美实现八种js焦点轮播图(下篇)
Apr 20 Javascript
Angular 页面跳转时传参问题
Aug 01 Javascript
jQuery动态创建元素以及追加节点的实现方法
Oct 20 Javascript
详解webpack编译多页面vue项目的配置问题
Dec 11 Javascript
Vue父子之间值传递的实例教程
Jul 02 Javascript
如何使用vue3打造一个物料库
May 08 Vue.js
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
BBS(php &amp; mysql)完整版(七)
2006/10/09 PHP
?生?D片??C字串
2006/12/06 PHP
php Smarty 字符比较代码
2011/02/27 PHP
PHP基础学习小结
2011/04/17 PHP
codeigniter中实现一次性加载多个view的方法
2015/03/20 PHP
PHP QRCODE生成彩色二维码的方法
2016/05/19 PHP
php运行报错Call to undefined function curl_init()的最新解决方法
2016/11/20 PHP
thinkphp 框架数据库切换实现方法分析
2020/05/18 PHP
利用javascript移动div层-javascript 拖动层
2009/03/22 Javascript
通过JS 获取Mouse Position(鼠标坐标)的代码
2009/09/21 Javascript
用Javascript同时提交多个Web表单的方法
2009/12/26 Javascript
javascript 鼠标悬浮图片显示原图 移出鼠标后原图消失(多图)
2009/12/28 Javascript
js location.replace与location.reload的区别
2010/09/08 Javascript
基于jquery实现拆分姓名的方法(纯JS版)
2013/05/08 Javascript
简单的js表单验证函数
2013/10/28 Javascript
asp.net刷新本页面的六种方法总结
2014/01/07 Javascript
Javascript基础知识(三)BOM,DOM总结
2014/09/29 Javascript
浅谈MUI框架中加载外部网页或服务器数据的方法
2018/01/31 Javascript
微信小程序利用云函数获取手机号码
2019/12/17 Javascript
Vue实现手机扫描二维码预览页面效果
2020/05/28 Javascript
python解析xml文件实例分析
2015/05/27 Python
python中找出numpy array数组的最值及其索引方法
2018/04/17 Python
使用python批量读取word文档并整理关键信息到excel表格的实例
2018/11/07 Python
Python实现将字符串的首字母变为大写,其余都变为小写的方法
2019/06/11 Python
PyTorch之图像和Tensor填充的实例
2019/08/18 Python
python中使用you-get库批量在线下载bilibili视频的教程
2020/03/10 Python
Django 解决阿里云部署同步数据库报错的问题
2020/05/14 Python
Pandas把dataframe或series转换成list的方法
2020/06/14 Python
基于keras中的回调函数用法说明
2020/06/17 Python
python实现登录与注册系统
2020/11/30 Python
AmazeUI 折叠面板的实现代码
2020/08/17 HTML / CSS
阿联酋电子产品购物网站:Menakart
2017/09/15 全球购物
经典演讲稿范文
2013/12/30 职场文书
工作时间上网检讨书
2014/02/03 职场文书
大二学习计划书范文
2014/04/27 职场文书
2016春季运动会前导词
2015/11/25 职场文书