指定时间内前进/后退n秒

2019-01-18  本文已影响0人  于美美

转自:https://www.cnblogs.com/xzsty/p/6665038.html

1.定义函数
reduceOneHouer(dateStr,time){
    var dt=new Date(dateStr.replace(/-/,"/"));
    var ndt=new Date(dt.getTime()+time);
    const year = parseInt(ndt.getFullYear())
    const month = parseInt(ndt.getMonth()+1)>9?parseInt(ndt.getMonth()+1):'0'+parseInt(ndt.getMonth()+1)
    const day = parseInt(ndt.getDate())>9?parseInt(ndt.getDate()):'0'+parseInt(ndt.getDate())
    const hour = parseInt(ndt.getHours())>9?parseInt(ndt.getHours()):'0'+parseInt(ndt.getHours())
    const minute = parseInt(ndt.getMinutes())>9?parseInt(ndt.getMinutes()):'0'+parseInt(ndt.getMinutes())
    const second = parseInt(ndt.getSeconds())>9?parseInt(ndt.getSeconds()):'0'+parseInt(ndt.getSeconds())
    const result = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second
    return result;
}
2.调用
reduceOneHouer('2019-02-01 18:10:10',Number(-1000*60*60))
//后退一小时  "2019-02-01 17:10:10"
3.获取当前时间
getNow(){
    const date=new Date();
    const year=date.getFullYear(); //获取当前年份
    const mon=date.getMonth()+1; //获取当前月份
    const day=date.getDate(); //获取当前日
    const hour=date.getHours(); //获取小时
    const m=date.getMinutes(); //获取分钟
    const s=date.getSeconds(); //获取秒
    const result = year +'-'+ mon +'-'+ day +' '+ hour +':'+ m +':'+ s
    return result
}
上一篇下一篇

猜你喜欢

热点阅读