search

Home  >  Q&A  >  body text

javascript - js about time conversion and time addition and subtraction.

1. Conversion of time

默认


mounted () {
  this.HomePageDisplay();
  var myDate = new Date();
  var reg=/[\u4E00-\u9FA5]/g;
  this.latestS=myDate.toLocaleString().replace(/\//g,'-').replace(/:/g,'-').replace(/[ ]/g,"").replace(reg,'');
  console.log(this.latestS)
},

I converted this.

What I want is 2017-07-05-04. .

2. Regarding the addition and subtraction of time.


1、2017-07-05-04  减去10个小时。 格式不变。。

2、 当前的小时减去10个小时。。    下面代码我写的。 给自己蠢哭了。 求指点。。
    


 var timeData = [
    myDate.getHours()-10, myDate.getHours()-9, myDate.getHours()-8, myDate.getHours()-7, myDate.getHours()-6, myDate.getHours()-5, myDate.getHours()-4, myDate.getHours()-3, myDate.getHours()-2, myDate.getHours()-1, myDate.getHours(),
  ];
phpcn_u1582phpcn_u15822747 days ago862

reply all(4)I'll reply

  • 迷茫

    迷茫2017-07-05 10:42:20

    For time processing, it is recommended to introduce the moment library. http://momentjs.com/docs/#/pa...

    var t = moment().format('YYYY-MM-DD-HH');
    console.log(t); //2017-07-03-18
    var tsub = moment(t, 'YYYY-MM-DD-HH').subtract(10, 'hours').format('YYYY-MM-DD-HH');
    console.log(tsub); //2017-07-03-08

    reply
    0
  • 阿神

    阿神2017-07-05 10:42:20

    1. If you need the format of 2017-03-14-04-50-08:

      var str = '2017/3/14 4:50:08 PM';
      var arr = str.split(/[ ^d ]+/g).map(item => (parseInt(item, 10) < ; 10 ? '0' + parseInt(item, 10) : item )).join('-');
      console.log(arr);

    2. As for the addition and subtraction of time, as mentioned above, it is actually good to introduce libraries such as moment.js into the project.

    reply
    0
  • PHP中文网

    PHP中文网2017-07-05 10:42:20

    Date.prototype.format = function(fmt) { 
         var o = { 
            "M+" : this.getMonth()+1,                 //月份 
            "d+" : this.getDate(),                    //日 
            "h+" : this.getHours(),                   //小时 
            "m+" : this.getMinutes(),                 //分 
            "s+" : this.getSeconds(),                 //秒 
            "q+" : Math.floor((this.getMonth()+3)/3), //季度 
            "S"  : this.getMilliseconds()             //毫秒 
        }; 
        if(/(y+)/.test(fmt)) {
                fmt=fmt.replace(RegExp., (this.getFullYear()+"").substr(4 - RegExp..length)); 
        }
         for(var k in o) {
            if(new RegExp("("+ k +")").test(fmt)){
                 fmt = fmt.replace(RegExp., (RegExp..length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
             }
         }
        return fmt; 
    }
    new Date().format("yyyy-MM-dd hh:mm:ss");//"2017-07-03 17:01:35"
    
    加减法用总的毫秒数计算,然后在转化成时间。   

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-07-05 10:42:20

    Date.prototype.past = function(pattern, pastDays) {

    function zeroize(num) {
        return num < 10 ? "0" + num : num;
    };
    var pastday = new Date((this - 0) - 1000 * 60 * 60 * 24 * pastDays);
    var pattern = pattern; //    YYYY-MM-DD 或 MM-DD-YYYY 或 YYYY-MM-DD , hh : mm : ss
    var dateObj = {
        "Y": pastday.getFullYear(),
        "M": zeroize(pastday.getMonth() + 1),
        "D": zeroize(pastday.getDate()),
        "h": zeroize(pastday.getHours()),
        "m": zeroize(pastday.getMinutes()),
        "s": zeroize(pastday.getSeconds())
    };
    return pattern.replace(/YYYY|MM|DD|hh|mm|ss/g, function(match) {
        switch (match) {
            case "YYYY":
                return dateObj.Y;
            case "MM":
                return dateObj.M;
            case "DD":
                return dateObj.D;
            case "hh":
                return dateObj.h;
            case "mm":
                return dateObj.m;
            case "ss":
                return dateObj.s;
        };
    });

    };
    var timeEnd = new Date();
    timeEnd = timeEnd.getFullYear() + '/' + (timeEnd.getMonth() + 1) + '/' + timeEnd.getDate() + ' ' + timeEnd. getHours() + ':' + timeEnd.getMinutes();
    You mean to push the current time forward 10 minutes, new Date(timeEnd).past('YYYY-MM-DD hh : mm : ss', 1/ (24*6));

    reply
    0
  • Cancelreply