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中的浮点型运算问题
Jan 06 Javascript
一个JavaScript操作元素定位元素的实例
Oct 29 Javascript
BootStrap中Datepicker控件带中文的js文件
Aug 10 Javascript
JavaScript 数组的深度复制解析
Nov 02 Javascript
网页挂马方式整理及详细介绍
Nov 03 Javascript
写jQuery插件时的注意点
Feb 20 Javascript
Bootstrap页面标题Page Header的实现方法
Mar 22 Javascript
js实现首屏延迟加载实现方法 js实现多屏单张图片延迟加载效果
Jul 17 Javascript
JS中获取 DOM 元素的绝对位置实例详解
Apr 23 Javascript
三种Webpack打包方式(小结)
Sep 19 Javascript
解决layui 三级联动下拉框更新时回显的问题
Sep 03 Javascript
Vue组件更新数据v-model不生效的解决
Apr 02 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
php模板函数 正则实现代码
2012/10/15 PHP
php代码书写习惯优化小结
2013/06/20 PHP
使用phpQuery获取数组的实例
2017/03/13 PHP
Laravel中前端js上传图片到七牛云的示例代码
2017/09/04 PHP
laravel 时间格式转时间戳的例子
2019/10/11 PHP
JavaScript易错知识点整理
2016/12/05 Javascript
bootstrap中的 form表单属性role=&quot;form&quot;的作用详解
2017/01/20 Javascript
Js经典案例的实例代码
2018/05/10 Javascript
原生JS实现获取及修改CSS样式的方法
2018/09/04 Javascript
ES6 fetch函数与后台交互实现
2018/11/14 Javascript
浅析Vue.js 中的条件渲染指令
2018/11/19 Javascript
node运行js获得输出的三种方式示例详解
2020/07/02 Javascript
编写Python脚本来获取mp3文件tag信息的教程
2015/05/04 Python
Python selenium如何设置等待时间
2016/09/15 Python
python3调用百度翻译API实现实时翻译
2018/08/16 Python
Python设计模式之建造者模式实例详解
2019/01/17 Python
pandas条件组合筛选和按范围筛选的示例代码
2019/08/26 Python
PyCharm安装PyQt5及其工具(Qt Designer、PyUIC、PyRcc)的步骤详解
2020/11/02 Python
马来西亚太阳镜、眼镜和隐形眼镜网上商店:Focus Point
2018/12/13 全球购物
正宗的日本零食和糖果订阅盒:Bokksu
2019/11/21 全球购物
个人自我评价分享
2013/12/20 职场文书
大学毕业感言200字
2014/03/09 职场文书
中秋寄语大全
2014/04/11 职场文书
学校花圃的标语
2014/06/18 职场文书
2014业务员年终工作总结
2014/12/09 职场文书
党性分析材料格式
2014/12/19 职场文书
个人工作能力自我评价
2015/03/05 职场文书
2015年“7.11”世界人口日宣传活动方案
2015/05/06 职场文书
交通事故案件代理词
2015/05/23 职场文书
行政二审代理词
2015/05/25 职场文书
css实现文章分割线样式的多种方法总结
2021/04/21 HTML / CSS
教你如何使用Python下载B站视频的详细教程
2021/04/29 Python
只用20行Python代码实现屏幕录制功能
2021/06/02 Python
Golang Elasticsearches 批量修改查询及发送MQ
2022/04/19 Golang
mysql数据库实现设置字段长度
2022/06/10 MySQL
Java 多线程并发FutureTask
2022/06/28 Java/Android