js擷取目前時間並轉變格式__js

來源:互聯網
上載者:User
/*** @description 把目前時間轉成 (年.月.日 時:分:秒)日期格式的* @params 無* @return 返回目前時間的日期格式,例如:2017.07.11 15:14:44 */function getCurrentTime(){    var date = new Date();      var month = date.getMonth() + 1;    var strDate = date.getDate();    if (month >= 1 && month <= 9) {         month = "0" + month;    }    if (strDate >= 0 && strDate <= 9) {         strDate = "0" + strDate;    }     var hours = date.getHours();    if (hours >=0 && hours <= 9) {        if (hours == 0) {            hours = "00";        } else{            hours = "0" + hours;        }    }    var minutes = date.getMinutes();    if (minutes >=0 && minutes <= 9) {        if (minutes == 0) {            minutes = "00";        } else{            minutes = "0" + minutes;        }    }    var seconds = date.getSeconds();    if (seconds >=0 && seconds <= 9) {        if (seconds == 0) {            seconds = "00";        } else{            seconds = "0" + seconds;        }    }var currentdate = date.getFullYear()+"."+ month+"."+strDate+" "+hours+":"+minutes+":"+ ":"+seconds;console.log(currentdate);   //2017.07.11 15:14:44return currentdate ;}
/*** @description 把傳入的(年 月 日 時 分 秒 2017 7 5 13 8 5)轉成無格式的日期(20170705130805)* @params year,month,strDate,hours,minutes,seconds 年 月 日 時 分 秒 例如:2017 7 5 13 8 5* @return 返回傳入參數的無格式日期 例如:20170705130805 */ function getCurrentTime(year,month,strDate,hours,minutes,seconds) {                if(month >= 1 && month <= 9) {                    month = "0" + month;                }                if(strDate > 0 && strDate <= 9) {                    strDate = "0" + strDate;                }                if(hours >= 0 && hours <= 9) {                    if (hours == 0) {                        hours = "00";                    } else{                        hours = "0" + hours;                    }                }                if(minutes >= 0 && minutes <= 9) {                    if (minutes == 0) {                        minutes = "00";                    } else{                        minutes = "0" + minutes;                    }                }                if(seconds >= 0 && seconds <= 9) {                    if (seconds == 0) {                        seconds = "00";                    } else{                        seconds = "0" + seconds;                    }                }                var currentdate = year + month + strDate + hours + minutes + seconds;                console.log(currentdate);                return currentdate;}
/*** @description 方法入口  要求逾時時間格式為20170705130805* @params tiemOut 單位秒 例如:60 (60秒後逾時)* @return  */ function sendRequest(){    var curTime = new Date();    var curTimeB = getCurrentTime(curTime.getFullYear(),curTime.getMonth()+1,curTime.getDate(),curTime.getHours(),curTime.getMinutes(),curTime.getSeconds());    console.log("目前時間="+curTimeB);//列印這裡為了對比轉變後的逾時時間                var oldTime = curTime .getTime(); //擷取目前時間的毫秒值                  //假如在1分鐘後逾時,轉成毫秒值                var timeOut = oldTime + 60 * 1000;//假如逾時時間為目前時間之後的60秒                console.log(timeOut);                //擷取逾時時間的時間戳記                var timeOutDate = new Date(timeOut);                console.log(timeOutDate);                //擷取逾時時間的年月日時分秒                var year = timeOutDate.getFullYear();                var month = timeOutDate.getMonth()+1;                var strDate = timeOutDate.getDate();                var hours = timeOutDate.getHours();                var minutes = timeOutDate.getMinutes();                var seconds = timeOutDate.getSeconds();                //轉變成需要的日期格式。                var time = getCurrentTime(year,month,strDate,hours,minutes,seconds);                console.log("逾時時間="+time);//20170705130805}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.