JS如何對日期操作封裝

這篇文章主要介紹了JS如何對日期操作封裝,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

我們提供的服務(wù)有:成都做網(wǎng)站、網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、南海ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的南海網(wǎng)站制作公司

格式化日期:

/**
 * 格式化日期
 * @param fmt 例如:yyyy-MM-dd 等
 * @returns {*}
 * @constructor
 */
Date.prototype.Format = function (fmt) { //author: meizz
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小時
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
};

得到日期年月日等 加數(shù)字后的日期:

/**
 * 得到日期年月日等 加數(shù)字后的日期
 * @param interval 指定類型 例如 y:代表年
 * @param number 需要增加 || 減少的 值
 * @param fmt 格式化日期 可選參數(shù) 默認(rèn):Thu Oct 24 2019 16:57:12 GMT+0800 (中國標(biāo)準(zhǔn)時間) 格式
 * @returns {Date}
 */
Date.prototype.dateAdd = function(interval,number,fmt)
{
  var d = this;
  var k={'y':'FullYear', 'q':'Month', 'm':'Month', 'w':'Date', 'd':'Date', 'h':'Hours', 'n':'Minutes', 's':'Seconds', 'ms':'MilliSeconds'};
  var n={'q':3, 'w':7};
  eval('d.set'+k[interval]+'(d.get'+k[interval]+'()+'+((n[interval]||1)*number)+')');
  if (fmt){
    return d.Format(fmt);
  }
  return d;
};

計算兩日期相差的日期年月日等:

/**
 * 計算兩日期相差的日期年月日等
 * @param interval 指定查看的類型 例如:y:代表年
 * @param objDate2 objDate2 < this = 負(fù)數(shù)
 * @returns {*}
 */
Date.prototype.dateDiff = function(interval,objDate2)
{
  var d=this, i={}, t=d.getTime(), t2=objDate2.getTime();
  i['y']=objDate2.getFullYear()-d.getFullYear();
  i['q']=i['y']*4+Math.floor(objDate2.getMonth()/4)-Math.floor(d.getMonth()/4);
  i['m']=i['y']*12+objDate2.getMonth()-d.getMonth();
  i['ms']=objDate2.getTime()-d.getTime();
  i['w']=Math.floor((t2+345600000)/(604800000))-Math.floor((t+345600000)/(604800000));
  i['d']=Math.floor(t2/86400000)-Math.floor(t/86400000);
  i['h']=Math.floor(t2/3600000)-Math.floor(t/3600000);
  i['n']=Math.floor(t2/60000)-Math.floor(t/60000);
  i['s']=Math.floor(t2/1000)-Math.floor(t/1000);
  return i[interval];
};

演示:

console.log(new Date('2019-10-23').dateAdd('y',1,'yyyy-MM-dd')) 結(jié)果為:2020-10-23

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“JS如何對日期操作封裝”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

文章標(biāo)題:JS如何對日期操作封裝
本文路徑:http://bm7419.com/article34/igeepe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計域名注冊、外貿(mào)建站、標(biāo)簽優(yōu)化網(wǎng)站排名、虛擬主機

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)