您好,登錄后才能下訂單哦!
小編給大家分享一下JS如何實(shí)現(xiàn)獲取時(shí)間已經(jīng)時(shí)間與時(shí)間戳轉(zhuǎn)換,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
function getCurrentMonthFirst=()=>{ var date=new Date(); date.setDate(1); return common.getdateNoTime(date); }
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; }
/** * @zhiparam sDate1 開始日期 如: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; }
/** * @zhiparam startDate 開始日期 如: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; }
/** *{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 month3 = parseInt(month) - monthNum; if (month3 <= 0) { var absM = Math.abs(month3); year2 = parseInt(year2) - Math.ceil(absM / 12 == 0 ? 1 : parseInt(absM) / 12); month3 = 12 - (absM % 12); } var day2 = day; var days2 = new Date(year2, month3, 0); days2 = days2.getDate(); if (day2 > days2) { day2 = days2; } if (month3 < 10) { month3 = '0' + month3; } var t2 = year2 + '-' + month3 + '-' + day2; return t2; }
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); }
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); }
function getdateTime= (date) => { var now = new Date(date), y = now.getFullYear(), m = now.getMonth() + 1, d = now.getDate(); return now.toTimeString().substr(0, 8); }
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; }
以上是“JS如何實(shí)現(xiàn)獲取時(shí)間已經(jīng)時(shí)間與時(shí)間戳轉(zhuǎn)換”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。