JS 中關於日期

來源:互聯網
上載者:User

標籤:parse   substring   gets   split   [1]   end   min   標準   span   

Fri Oct 31 18:00:00 UTC+0800 2008 轉換  2008-10-31

 

function Todate(num) { //Fri Oct 31 18:00:00 UTC+0800 2008     num = num + "";    var date = "";    var month = new Array();    month["Jan"] = 1; month["Feb"] = 2; month["Mar"] = 3; month["Apr"] = 4; month["May"] = 5; month["Jan"] = 6;    month["Jul"] = 7; month["Aug"] = 8; month["Sep"] = 9; month["Oct"] = 10; month["Nov"] = 11; month["Dec"] = 12;    var week = new Array();    week["Mon"] = "一"; week["Tue"] = "二"; week["Wed"] = "三"; week["Thu"] = "四"; week["Fri"] = "五"; week["Sat"] = "六"; week["Sun"] = "日";    str = num.split(" ");    date = str[5] + "-";    date = date + month[str[1]] + "-" + str[2];    return date;}  

 

將時間戳記處理成標準格式

 

function time_deal(time_stamp){    var year = time_stamp.substring(0,4);      var month = time_stamp.substring(4,6);      var day = time_stamp.substring(6,8);      var new_time = year+"-"+month+"-"+day;      return new_time;}

 

計算兩個日期相差多少天

/** * 計算兩個日期相差多少天 * @param strDateStart * @param strDateEnd * @returns */function getDays(strDateStart,strDateEnd){       var strSeparator = "-"; //日期分隔符號       var oDate1;       var oDate2;       var iDays;       oDate1= strDateStart.split(strSeparator);       oDate2= strDateEnd.split(strSeparator);       var strDateS = new Date(oDate1[0], oDate1[1]-1, oDate1[2]);       var strDateE = new Date(oDate2[0], oDate2[1]-1, oDate2[2]);       iDays = parseInt(Math.abs(strDateS - strDateE ) / 1000 / 60 / 60 /24)//把相差的毫秒數轉換為天數        return iDays ;}

擷取目前時間

 

/** * 擷取目前時間 */function getNowFormatDate() {    var date = new Date();    var seperator1 = "-";    var seperator2 = ":";    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 currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;//            + " " + date.getHours() + seperator2 + date.getMinutes()//            + seperator2 + date.getSeconds();    return currentdate;}

 

JS 中關於日期

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.