JS 时间处理(GMT转换,超24小时加一天,时间差计算)

计算天数,加小时,加分数

Date.prototype.Format = function (fmt) { // author: meizz
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.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
var nowTime = new Date().Format("yyyy-MM-dd hh:mm:ss"); //当前时间
//console.log(nowTime)

var tenMinutes= new Date(nowTime);
tenMinutes.setMinutes(tenMinutes.getMinutes()-30); //当前时间加10分钟
var tenmin = tenMinutes.Format("yyyy-MM-dd hh:mm:ss");



<script type="text/javascript">
    function f() {
        var a = "2019-07-11T16:54:36.193";
        var b = a.replace("T"," ");
        var c = b.substring(0,16)
        console.log(c);
    }

    //时间提取处理
    f();
    
    var date1= '2019-07-01 01:09:00';  //开始时间
    var date2 = '2019-07-11 02:12:00';    //结束时间
    var date3 = new Date(date2).getTime() - new Date(date1).getTime();   //时间差的毫秒数
    //计算出相差天数
    var days=Math.floor(date3/(24*3600*1000))
    //计算出小时数
    var leave1=date3%(24*3600*1000)    //计算天数后剩余的毫秒数
    var hours=Math.floor(leave1/(3600*1000))
    //计算相差分钟数
    var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数
    var minutes=Math.floor(leave2/(60*1000))
    //计算相差秒数
    var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
    var seconds=Math.round(leave3/1000)
    console.log(" 相差 "+days+"天 "+hours+"小时 "+minutes+" 分钟"+seconds+" 秒")
    console.log(Math.round(((days*24)+hours)/30))
</script>


<script>
    //GMT转普通格式的方法
    function GMTToStr(time){
        let date = new Date(time)
        let Str=date.getFullYear() + '-' +
            (date.getMonth() + 1) + '-' +
            date.getDate() + ' ' +
            date.getHours() + ':' +
            date.getMinutes() + ':' +
            date.getSeconds()
        return Str

    }
    function judgFailTime() {
        var x = "2019-07-18 23:25:26";
        var times = new Date(x.replace("-","/"));
        var b = 15; //分钟数
        var c = 5; //小时数
        times.setHours(times.getHours()+c);    //小时
        //times.setMinutes(times.getMinutes() + b),     //分种
        //times.getSeconds(), 0);   //秒数
        //console.log(times);

        //GMT转普通格式
        let DateTime=times;
        let a=this.GMTToStr(DateTime);
        console.log(a);
    }

    judgFailTime()
</script>
<script>
    var stringTime = '2019-07-15T11:08:13.681648';
    var timestamp2 = Date.parse(new Date(stringTime));
    timestamp2 = timestamp2 / 1000;
    var seconds = 2*3600;
    timestamp=timestamp2+seconds
    console.log(stringTime + "的时间戳为:" + timestamp);
    var newDate = new Date();
    newDate.setTime(timestamp * 1000);
    console.log(newDate.toGMTString());
</script>

<script>
    //2019-07-10 10:10
    var mydate= new Date("2019-07-10 10:10");
    mydate.setDate(mydate.getDate()+1); //当前时间加1天
    //console.log(mydate)
    mydate.setMinutes(mydate.getMinutes()+20); //当前时间加20分钟
    mydate.setHours(mydate.getHours()+2) //当前时间加上小时
    //console.log(mydate)
</script>
 

 

posted @ 2019-07-19 09:37  隰荷华  阅读(3233)  评论(2编辑  收藏  举报