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对象和Javascript对象之间转换的实例代码
Mar 20 Javascript
Area 区域实现post提交数据的js写法
Apr 22 Javascript
javascript模拟php函数in_array
Apr 27 Javascript
JavaScript实现上下浮动的窗口效果代码
Oct 12 Javascript
几种经典排序算法的JS实现方法
Mar 25 Javascript
第三章之Bootstrap 表格与按钮功能
Apr 25 Javascript
基于wordpress的ajax写法详解
Jan 02 Javascript
解决npm安装Electron缓慢网络超时导致失败的问题
Feb 06 Javascript
基于vue的验证码组件的示例代码
Jan 22 Javascript
原生JS实现顶部导航栏显示按钮+搜索框功能
Dec 25 Javascript
Vue3 的响应式和以前有什么区别,Proxy 无敌?
May 20 Javascript
解决vue-cli输入命令vue ui没效果的问题
Nov 17 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中array_map与array_column之间的关系分析
2014/08/19 PHP
Codeigniter通过SimpleXML将xml转换成对象的方法
2015/03/19 PHP
PHP实现XML与数据格式进行转换类实例
2015/07/29 PHP
PHP程序员学习使用Swoole的理由
2018/06/24 PHP
Centos7.7 64位利用本地完整安装包安装lnmp/lamp套件教程
2021/03/09 Servers
来自chinaz的ajax获取评论代码
2008/05/03 Javascript
通过JS 获取Mouse Position(鼠标坐标)的代码
2009/09/21 Javascript
JQuery选择器特辑 详细小结
2012/05/14 Javascript
解决jquery操作checkbox火狐下第二次无法勾选问题
2014/02/10 Javascript
JS生成随机字符串的多种方法
2014/06/10 Javascript
jQuery实现简易的天天爱消除小游戏
2015/10/16 Javascript
JavaScript多并发问题如何处理
2015/10/28 Javascript
Vue.js实现无限加载与分页功能开发
2016/11/03 Javascript
用JavaScript和jQuery实现瀑布流
2017/03/19 Javascript
关于页面刷新vuex数据消失问题解决方案
2017/07/03 Javascript
Vue之Watcher源码解析(2)
2017/07/19 Javascript
JavaScript字符串转数字的5种方法及遇到的坑
2018/07/16 Javascript
vue实现压缩图片预览并上传功能(promise封装)
2019/01/10 Javascript
js实现倒计时器自定义时间和暂停
2019/02/25 Javascript
jquery登录的异步验证操作示例
2019/05/09 jQuery
微信分享invalid signature签名错误踩过的坑
2020/04/11 Javascript
js实现自定义右键菜单
2020/05/18 Javascript
Vue执行方法,方法获取data值,设置data值,方法传值操作
2020/08/05 Javascript
[02:36]DOTA2英雄基础教程 一击致命幻影刺客
2013/12/06 DOTA
python多线程threading.Lock锁用法实例
2014/11/01 Python
详解Python中的变量及其命名和打印
2016/03/11 Python
Python实现的十进制小数与二进制小数相互转换功能
2017/10/12 Python
Python中的正则表达式与JSON数据交换格式
2019/07/03 Python
python设置环境变量的作用和实例
2019/07/09 Python
使用Pytorch搭建模型的步骤
2020/11/16 Python
python 合并多个excel中同名的sheet
2021/01/22 Python
6种非常炫酷的CSS3按钮边框动画特效
2016/03/16 HTML / CSS
html5响应式开发自动计算fontSize的方法
2020/01/13 HTML / CSS
小学英语教学反思
2014/01/30 职场文书
护士年终考核评语
2014/12/31 职场文书
失职检讨书大全
2015/01/26 职场文书