JavaScript通过Date-Mask将日期转换成字符串的方法_javascript技巧

本文实例讲述了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
, 字符串
, 日期
Date-Mask
data datemask属性、datemask、zbrush mask 技巧、javascript date、javascript new date,以便于您获取更多的相关知识。

时间: 2024-09-15 20:05:33

JavaScript通过Date-Mask将日期转换成字符串的方法_javascript技巧的相关文章

JavaScript通过Date-Mask将日期转换成字符串的方法

  本文实例讲述了JavaScript通过Date-Mask将日期转换成字符串的方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 6

JavaScript实现将UPC转换成ISBN的方法_javascript技巧

本文实例讲述了JavaScript实现将UPC转换成ISBN的方法.分享给大家供大家参考.具体实现方法如下: if (indexisbn.indexOf("978") == 0) { isbn = isbn.substr(3,9); var xsum = 0; var add = 0; var i = 0; for (i = 0; i < 9; i++) { add = isbn.substr(i,1); xsum += (10 - i) * add; } xsum %= 11;

js冒泡法和数组转换成字符串示例代码_javascript技巧

js代码: 复制代码 代码如下: window.onload = function(){ var mian = document.getElementById( "mian" ); var mian1 = document.getElementById( "mian1" ); var str = mian.innerHTML; var arry = []; var len = str.length; for( var i = 0; i < len; i++ )

js将json格式内容转换成对象的方法_javascript技巧

复制代码 代码如下: {"options":"[{\"text\":\"王家湾\",\"value\":\"9\"},{\"text\":\"李家湾\",\"valu e\":\"10\"},{\"text\":\"邵家湾\",\"value\":\"

PHP实现将颜色hex值转换成rgb的方法_php技巧

本文实例讲述了PHP实现将颜色hex值转换成rgb的方法.分享给大家供大家参考,具体如下: function hex2rgb( $colour ) { if ( $colour[0] == '#' ) { $colour = substr( $colour, 1 ); } if ( strlen( $colour ) == 6 ) { list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $co

PHP实现数组array转换成xml的方法_php技巧

本文实例讲述了PHP实现数组array转换成xml的方法.分享给大家供大家参考,具体如下: <?php $elementLevel = 0 ; function array_Xml($array, $keys = '') { global $elementLevel; if(!is_array($array)) { if($keys == ''){ return $array; }else{ return "\n<$keys>" . $array . "&l

php实现字符串首字母转换成大写的方法_php技巧

本文实例讲述了php实现字符串首字母转换成大写的方法.分享给大家供大家参考.具体分析如下: php中可以通过ucfirst函数将一个字符串中的第一个字母转换成大写,而ucwords函数可以将一个字符串中每个单词的首字母转换成大写 <?php $string = "php string functions are easy to use."; $sentence = ucfirst($string); $title = ucwords($string); print("$

php把数组值转换成键的方法_php技巧

本文实例讲述了php把数组值转换成键的方法.分享给大家供大家参考.具体如下: function values2keys($arr, $value=1){ $new = array(); while (list($k,$v) = each($arr)){ $v = trim($v); if ($v != ''){ $new[$v] = $value; } } return $new; } 希望本文所述对大家的php程序设计有所帮助. 以上是小编为您精心准备的的内容,在的博客.问答.公众号.人物.课

JavaScript跨浏览器获取页面中相同class节点的方法_javascript技巧

网页开发时,在很多时候我们需要操作相同类名的元素,即class相同的元素.昨天参加笔试,有一道相关的题目没答上来: JavaScript获取页面中class为test的节点 于是收集了一些相关的资料,在本文中列举了两种我觉得比较好的方法,不足之处,还望大家批评指正.如果大家有更好的方法,希望可以分享. Solution1 Jeremy Keuth方案 Jeremy Keuth大叔在<JavaScript DOM 编程艺术>(第2版)(英文:DOM Scripting-Web Design wi