【uniapp 开发】日期工具类 -- DateUtil

日期格式转毫秒值


var time = ‘2019-08-08 12:09:34’;
var time222 = time.replace("-", "/").replace("-", "/");
var timestamp = Date.parse(time222);

毫秒值转日期格式


    formatDate:function(date, fmt) {
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
         
        let o = {
            'M+': date.getMonth() + 1,
            'd+': date.getDate(),
            'h+': date.getHours(),
            'm+': date.getMinutes(),
            's+': date.getSeconds()
        };
         
        for (let k in o) {
            if (new RegExp(`(${k})`).test(fmt)) {
                let str = o[k] + '';
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str));
            }
        }
        return fmt;
    },

       padLeftZero:function(str) {
        return ('00' + str).substr(str.length);
    },

获取现在时刻的日期,星期几,时分秒

getTime: function(type){
        let date = new Date(),
        currentDate,
        currentTime,
        seperator = "-", // 如果想要其他格式 只需 修改这里 
        year = date.getFullYear(),
        month = date.getMonth() + 1,
        weex = date.getDay(),
        day = date.getDate(),
        hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
        minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
        second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        month >= 1 && month <= 9 ? (month = "0" + month) : "";
        day >= 0 && day <= 9 ? (day = "0" + day) : "";
          //当前 日期
         currentDate = year + seperator + month + seperator + day;
          //当前 时间
         currentTime = hour + ":" + minute + ":" + second;
          // 输出格式 为 2018-8-27 14:45:33
         // console.log(currentDate +" "+ currentTime);
        
         if(type==0){
             return currentDate
         }
         else if(type==1){
             return currentTime;
         }
         else if(type==2){
             if(weex==1){
                 return '星期一'
             }if(weex==2){
                 return '星期二'
             }if(weex==3){
                 return '星期三'
             }if(weex==4){
                 return '星期四'
             }if(weex==5){
                 return '星期五'
             }if(weex==6){
                 return '星期六'
             }if(weex==7){
                 return '星期日'
             }
         }
         else{
            return currentDate+" "+ currentTime;
         }
},

获取当前时间戳

var time = (new Date()).getTime();
console.log('time=='+time);

两个日期间隔天数

getDays: function(date1, date2) {
    const dateTime = 1000 * 60 * 60 * 24; //每一天的毫秒数
    const minusDays = Math.floor(((date2.getTime() - date1.getTime()) / dateTime));//计算出两个日期的天数差
    return Math.abs(minusDays);
},


var time = this.getDays(new Date('2019-08-09'),new Date('2019-07-09'));
console.log('time=='+time);

获取10天后的日期

function daysAddSub(date, num) {
            let d = new Date(date.getTime());
            let newD = new Date(d.setDate(d.getDate() + num));//设置天数 +1 天
            return formatDate(newD, 'yyyyMMdd');
}


console.log(this.daysAddSub(new Date('2018/06/28'),10));

获取某一年的第一天的日期


_getFirstDayOfYear: function(date) {
       var year = this._getYear(date);
       var dateString = year + "-01-01 00:00:00";
       return dateString;
},

_getYear: function(date) {
        return this._transferDate(date).getFullYear();
},

某个日期是在当月中的星期几

_getDayOfWeek: function(date1) {
       return this._getWeek(date1);
},

 /*获取今天星期几,如果为0代表星期日*/
_getWeek: function(date) {
       return this._transferDate(date).getDay();
},

 /*转换日期*/
_transferDate: function(date) {
       if(typeof date == "string") {
              return new Date(date.replace(/-/ig, "/"));
       } else {
              return date;
        }
},

某个日期是否晚于某个日期

 _after: function(date1, date2) {
       var stime = this._getTime(this._transferDate(date1));
       var etime = this._getTime(this._transferDate(date2));
       return stime < etime ? true : false;
},

/*返回 1970 年 1 月 1 日至今的毫秒数。*/
_getTime: function(date) {
      return this._transferDate(date).getTime();
},

 /*转换日期*/
_transferDate: function(date) {
        if(typeof date == "string") {
              return new Date(date.replace(/-/ig, "/"));
        } else {
              return date;
        }
},

转载于:https://www.cnblogs.com/neo-java/p/11321375.html

你可能感兴趣的:(【uniapp 开发】日期工具类 -- DateUtil)