欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript通過Date-Mask將日期轉(zhuǎn)換成字符串的方法

 更新時間:2015年06月04日 17:08:51   作者:不吃皮蛋  
這篇文章主要介紹了JavaScript通過Date-Mask將日期轉(zhuǎn)換成字符串的方法,涉及javascript日期、數(shù)組及字符串操作的相關(guān)技巧,需要的朋友可以參考下

本文實例講述了JavaScript通過Date-Mask將日期轉(zhuǎn)換成字符串的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

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程序設(shè)計有所幫助。

相關(guān)文章

  • 原生javascript和jquery判斷瀏覽器版本等信息

    原生javascript和jquery判斷瀏覽器版本等信息

    本文為大家詳細介紹下通過jquery和原生javascript判斷瀏覽器信息包括:判斷瀏覽器是否為IE以及IE版本是多少等等,感興趣的朋友可以參考下哈,希望對大家有所幫助
    2013-07-07
  • JS實現(xiàn)轉(zhuǎn)動隨機數(shù)抽獎特效代碼

    JS實現(xiàn)轉(zhuǎn)動隨機數(shù)抽獎特效代碼

    這篇文章主要為大家詳細介紹了一款轉(zhuǎn)動隨機數(shù)抽獎的JS特效代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-08-08
  • javascript實現(xiàn)詳細時間提醒信息效果的方法

    javascript實現(xiàn)詳細時間提醒信息效果的方法

    這篇文章主要介紹了javascript實現(xiàn)詳細時間提醒信息效果的方法,涉及javascript操作時間的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • javascript實現(xiàn)無縫上下滾動特效

    javascript實現(xiàn)無縫上下滾動特效

    這篇文章主要介紹了javascript實現(xiàn)無縫上下滾動特效的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • js實現(xiàn)圖片推拉門效果代碼實例

    js實現(xiàn)圖片推拉門效果代碼實例

    這篇文章主要介紹了js實現(xiàn)圖片推拉門效果代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • JavaScript學(xué)習(xí)小結(jié)(一)——JavaScript入門基礎(chǔ)

    JavaScript學(xué)習(xí)小結(jié)(一)——JavaScript入門基礎(chǔ)

    本教程比較適合javascript初學(xué)者,對javascript基本知識的小結(jié)包括變量,基本類型等知識點,需要的朋友一起來學(xué)習(xí)吧
    2015-09-09
  • 網(wǎng)站內(nèi)容禁止復(fù)制和粘貼、另存為的js代碼

    網(wǎng)站內(nèi)容禁止復(fù)制和粘貼、另存為的js代碼

    這篇文章主要介紹了JS如何實現(xiàn)網(wǎng)站內(nèi)容如何實現(xiàn)禁止復(fù)制和粘貼、另存為,需要的朋友可以參考下
    2014-02-02
  • JS解密入門之憑直覺解

    JS解密入門之憑直覺解

    什么叫直覺解?根據(jù)目前流行的免殺方法,有這么幾種初級加密方法。進制加密,URLEncode加密,加花
    2008-06-06
  • JavaScript改變函數(shù)作用域的方法示例

    JavaScript改變函數(shù)作用域的方法示例

    本文主要介紹了JavaScript改變函數(shù)作用域的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • webpack proxy如何解決跨域問題

    webpack proxy如何解決跨域問題

    webpack proxy,即webpack提供的代理服務(wù),基本行為就是接收客戶端發(fā)送的請求后轉(zhuǎn)發(fā)給其他服務(wù)器,本文給大家介紹了webpack proxy如何解決跨域問題,感興趣的朋友跟隨小編一起看看吧
    2023-10-10

最新評論