亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JS實(shí)現(xiàn)獲取時(shí)間已經(jīng)時(shí)間與時(shí)間戳轉(zhuǎn)換

 更新時(shí)間:2022年03月10日 10:34:03   作者:小小陳丶  
這篇文章主要為大家提供了用JavaScript編寫(xiě)的獲取時(shí)間的類(lèi),以及時(shí)間戳轉(zhuǎn)時(shí)間的三種格式,文中的示例代碼講解詳細(xì),感興趣的可以了解一下

時(shí)間戳轉(zhuǎn)換時(shí)間或獲取日期工具類(lèi)

獲取當(dāng)前月的第一天

function getCurrentMonthFirst=()=>{
  var date=new Date();
  date.setDate(1);
  return common.getdateNoTime(date);
}

獲前取n天日期

function getBeforeDate=()=>{
  var n = n;
  var d = new Date();
  var year = d.getFullYear();
  var mon = d.getMonth() + 1;
  var day = d.getDate();
  if (day <= n) {
    if (mon > 1) {
      mon = mon - 1;
    } else {
      year = year - 1;
      mon = 12;
    }
  }
  d.setDate(d.getDate() - n);
  year = d.getFullYear();
  mon = d.getMonth() + 1;
  day = d.getDate();
  const s = year + '-' + (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day);
  return s;
}

根據(jù)兩個(gè)日期,判斷相差天數(shù)

/**
 * @zhiparam sDate1 開(kāi)始日期 如:2016-11-01
 * @param sDate2 結(jié)束日期 如:2016-11-02
 * @returns {nDays} 返回相差天數(shù)
 */
function daysBetween = (sDate1, sDate2) => {
  var time1 = Date.parse(new Date(sDate1));
  var time2 = Date.parse(new Date(sDate2));
  var nDays = Math.abs(parseInt((time2 - time1) / 1000 / 3600 / 24));
  return nDays;
}

根據(jù)bai兩個(gè)日期,判斷相差月數(shù)

/**
 * @zhiparam startDate 開(kāi)始日期 如:2016-11-01
 * @param endStart結(jié)束日期 如:2016-11-02
 * @returns {intervalMonth} 返回相差月數(shù)
 */
function getIntervalMonth = (startDate, endStart) => {
  var startMonth = new Date(startDate).getMonth();
  var endMonth = new Date(endStart).getMonth();
  var intervalMonth =
    new Date(endStart).getFullYear() * 12 + endMonth - (new Date(startDate).getFullYear() * 12 + startMonth);
  return intervalMonth;
}

獲取幾個(gè)月前的輸入日期

/**
 *{param:DateTime} date 輸入日期(YYYY-MM-DD)
 *{param:number } monthNum 月數(shù)
 */
function getIntervalMonth = (startDate, endStart) => {
  var dateArr = date.split('-');
  var year = dateArr[0]; //獲取當(dāng)前日期的年份
  var month = dateArr[1]; //獲取當(dāng)前日期的月份
  var day = dateArr[2]; //獲取當(dāng)前日期的日
  var days = new Date(year, month, 0);
  days = days.getDate(); //獲取當(dāng)前日期中月的天數(shù)
  var year2 = year;
  var month2 = parseInt(month) - monthNum;
  if (month2 <= 0) {
    var absM = Math.abs(month2);
    year2 = parseInt(year2) - Math.ceil(absM / 12 == 0 ? 1 : parseInt(absM) / 12);
    month2 = 12 - (absM % 12);
  }
  var day2 = day;
  var days2 = new Date(year2, month2, 0);
  days2 = days2.getDate();
  if (day2 > days2) {
    day2 = days2;
  }
  if (month2 < 10) {
    month2 = '0' + month2;
  }
  var t2 = year2 + '-' + month2 + '-' + day2;
  return t2;
}

時(shí)間戳轉(zhuǎn)換時(shí)間

function getdate= (date) => {
  var now = new Date(date),
    y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate();
  return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d) + ' ' + now.toTimeString().substr(0, 8);
}

時(shí)間戳轉(zhuǎn)換時(shí)間 - 無(wú)時(shí)分秒

function getdateNoTime= (date) => {
  var now = new Date(date),
    y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate();
  return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
}

時(shí)間戳轉(zhuǎn)換時(shí)間-無(wú)日期

function getdateTime= (date) => {
  var now = new Date(date),
    y = now.getFullYear(),
    m = now.getMonth() + 1,
    d = now.getDate();
  return now.toTimeString().substr(0, 8);
}

獲取當(dāng)前日期

function formatting= (time) => {
  let date = new Date();
  if (time !== undefined) {
    date = new Date(time);
  }
  const seperator1 = '-';
  const year = date.getFullYear();
  let month = date.getMonth() + 1;
  let strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = `0${month}`;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = `0${strDate}`;
  }
  const currentdate = year + seperator1 + month + seperator1 + strDate;
  return currentdate;
}

到此這篇關(guān)于JS實(shí)現(xiàn)獲取時(shí)間已經(jīng)時(shí)間與時(shí)間戳轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)JS 時(shí)間 時(shí)間戳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JSON.stringify的多種用法總結(jié)

    JSON.stringify的多種用法總結(jié)

    這篇文章主要給大家介紹了關(guān)于JSON.stringify使用的相關(guān)資料, JSON.stringify()方法是將一個(gè)JavaScript值(對(duì)象或者數(shù)組)轉(zhuǎn)換為一個(gè) JSON字符串,需要的朋友可以參考下
    2021-06-06
  • JS校驗(yàn)與最終登陸界面功能完整示例

    JS校驗(yàn)與最終登陸界面功能完整示例

    這篇文章主要介紹了JS校驗(yàn)與最終登陸界面功能,結(jié)合完整實(shí)例形式詳細(xì)分析了JavaScript登錄界面校驗(yàn)功能與正則驗(yàn)證相關(guān)操作技巧,需要的朋友可以參考下
    2020-01-01
  • js圖片放大鏡效果實(shí)現(xiàn)方法詳解

    js圖片放大鏡效果實(shí)現(xiàn)方法詳解

    這篇文章主要為大家詳細(xì)介紹了js圖片放大鏡效果的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • JavaScript實(shí)現(xiàn)求最大公共子串的方法

    JavaScript實(shí)現(xiàn)求最大公共子串的方法

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)求最大公共子串的方法,涉及javascript針對(duì)字符串的遍歷、匹配、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下
    2018-02-02
  • 通過(guò)javascript獲取iframe里的值示例代碼

    通過(guò)javascript獲取iframe里的值示例代碼

    iframe里的值在實(shí)現(xiàn)一些比較特殊功能時(shí)需要獲取的,下面為大家詳細(xì)介紹下使用javascript獲取iframe里值的方法,感興趣的各位可以參考下哈
    2013-06-06
  • 微信小程序多音頻播放進(jìn)度條問(wèn)題

    微信小程序多音頻播放進(jìn)度條問(wèn)題

    小程序的音頻組件居然沒(méi)有進(jìn)度控制的功能,需要我們自己實(shí)現(xiàn),下面腳本之家小編給大家?guī)?lái)了微信小程序多音頻播放進(jìn)度條問(wèn)題,感興趣的朋友一起看看吧
    2018-08-08
  • JS異步執(zhí)行結(jié)果獲取的3種解決方式

    JS異步執(zhí)行結(jié)果獲取的3種解決方式

    這篇文章主要給大家介紹了關(guān)于JS異步執(zhí)行結(jié)果獲取的3種解決方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • JS異常處理的一個(gè)想法(sofish)

    JS異常處理的一個(gè)想法(sofish)

    由于網(wǎng)絡(luò)、瀏覽器問(wèn)題、緩存等原因執(zhí)行js的時(shí)候會(huì)拋出異常接下來(lái)分享一個(gè)想法,基本上涉及到兩步:收集和使用,感興趣的你可以參考下哈,希望對(duì)你有所幫助
    2013-03-03
  • 分享兩段簡(jiǎn)單的JS代碼防止SQL注入

    分享兩段簡(jiǎn)單的JS代碼防止SQL注入

    下面小編就為大家分享兩段簡(jiǎn)單的JS代碼防止SQL注入。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-04-04
  • js實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小功能

    js實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小功能

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11

最新評(píng)論