JS 中关于日期

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS 中关于日期相关的知识,希望对你有一定的参考价值。

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 中关于日期的主要内容,如果未能解决你的问题,请参考以下文章

JS代码片段:一个日期离现在多久了

Unity中关于AnimationEvent.Time的问题

JS中关于window对象的代码?

Python中关于日期相等的GQL查询

JAVA中关于日期的最常见的操作

oracle中关于日期的获取