js 各式各样的时间计算以及时间转换

IOSbug:!!!

1:UTC转24小时制时间--年月日 时分

function utc2beijing(utc_datetime) {  // UTC转时间
    function formatFunc(str) {    //格式化显示
        return str > 9 ? str : '0' + str
      }
      var UTCDateString= utc_datetime //2017-11-16 PM 01:23
      var date2 = new Date(UTCDateString);     //这步是关键
      var year = date2.getFullYear();
      var mon = formatFunc(date2.getMonth() + 1);
      var day = formatFunc(date2.getDate());
      var hour = date2.getHours();
      // var noon = hour >= 12 ? 'PM' : 'AM';
      // hour = hour>=12?hour-12:hour;
      // hour = formatFunc(hour);
      var min = formatFunc(date2.getMinutes());
      var dateStr = year+'-'+mon+'-'+day+' '+hour+':'+min;
      return dateStr
} 
复制代码

最后结果

utc2beijing("2018-12-18T23:36:26+08:00")
"2018-12-18 23:36"
复制代码

2:日期转换 Tue Jan 01 2019 09:59:18 GMT+0800 (UTC时间) ---> 2018-12-18--------------------

function changeDateType(date) {
    var now = new Date(date);
    var year = now.getFullYear();
    var honth = now.getMonth() + 1;
    var day = now.getDate();
    return `${year}-${honth}-${day}`
}
复制代码

3:几天前 - 几小时前 - 几秒前 jsDateDiff('2018/12/19 22:00:00')-----------------

    function jsDateDiff(pTime){   
        var d_minutes,d_hours,d_days,d;
        var timeNow = parseInt(new Date().getTime()/1000);
        pTime_new = new Date(pTime).getTime()/1000;
        d = timeNow - pTime_new;       
        d_days = parseInt(d/86400);       
        d_hours = parseInt(d/3600);       
        d_minutes = parseInt(d/60);    
        if(d_days>0 && d_days<4){       
            return d_days+"天前";       
        }else if(d_days<=0 && d_hours>0){       
            return d_hours+"小时前";       
        }else if(d_hours<=0 && d_minutes>0){       
            return d_minutes+"分钟前";       
        }else{
            return pTime;
        }
    }
复制代码

**4: 获取当前日期2018-12-18 ------------------------------ **

function getCurrentDate(format) {
    var now = new Date();
    var year = now.getFullYear(); //得到年份
    var month = now.getMonth(); //得到月份
    var date = now.getDate(); //得到日期
    var day = now.getDay(); //得到周几
    var hour = now.getHours(); //得到小时
    var minu = now.getMinutes(); //得到分钟
    var sec = now.getSeconds(); //得到秒
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minu < 10) minu = "0" + minu;
    if (sec < 10) sec = "0" + sec;
    var time = "";
    if (format == 1) { //精确到天
        time = year + "-" + month + "-" + date;
    } else if (format == 2) { //精确到分
        time = year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec;
    }
    return time;
}
复制代码

结果:getCurrentDate(1)精确到天 getCurrentDate(2)精确到分--------------

5:时间往前加,dateAdd(14) 14天后的时间------------------------------

function dateAdd(add) { 
    var dateTime = new Date();
    dateTime = dateTime.setDate(dateTime.getDate() + add);
    dateTime = new Date(dateTime);
    return dateTime
}
复制代码

** 结果: //dateAdd(14) 14天后 =》Tue Jan 01 2019 09:59:18 GMT+0800 (中国标准时间)-**

6:时间戳转化为年 月 日 时 分 秒---------------

function formatTime(number, format) {
    var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
    var returnArr = [];

    var date = new Date(number);
    returnArr.push(date.getFullYear() + '年');
    returnArr.push(formatNumber(date.getMonth() + 1 + '月'));
    returnArr.push(formatNumber(date.getDate() + '日'));
    returnArr.push(formatNumber(date.getHours()));
    returnArr.push(formatNumber(date.getMinutes()));
    returnArr.push(formatNumber(date.getSeconds()));
    // returnArr.push(formatNumber(date.getMilliseconds()));
    for (var i in returnArr) {
        format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;
}
复制代码

number: 传入时间戳;format:返回格式,支持自定义,但参数必须与formateArr里保持一致--------

7://获得两个日期之间相差的天数

function daysBetween(date1, date2) {
    var date1Str = date1.split("-"); //将日期字符串分隔为数组,数组元素分别为年.月.日
    //根据年 . 月 . 日的值创建Date对象
    var date1Obj = new Date(date1Str[0], (date1Str[1] - 1), date1Str[2]);
    var date2Str = date2.split("-");
    var date2Obj = new Date(date2Str[0], (date2Str[1] - 1), date2Str[2]);
    var t1 = date1Obj.getTime();
    var t2 = date2Obj.getTime();
    var dateTime = 1000 * 60 * 60 * 24; //每一天的毫秒数
    var minusDays = Math.floor(((t2 - t1) / dateTime)); //计算出两个日期的天数差
    var days = Math.abs(minusDays); //取绝对值
    return days;
}
// s1 = "2018-8-10"
// s2 = "2018-8-22"
// console.log(daysBetween(s1, s2))
复制代码

转载于:https://juejin.im/post/5c192f52f265da615c593289

你可能感兴趣的:(js 各式各样的时间计算以及时间转换)