javascript - js about time conversion and time addition and subtraction.
phpcn_u1582
phpcn_u1582 2017-07-05 10:40:20
0
4
802

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_u1582
phpcn_u1582

reply all(4)
迷茫

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
阿神
  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) &lt ; 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.

洪涛
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"

加减法用总的毫秒数计算,然后在转化成时间。   
我想大声告诉你

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));

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!