js获取指定时间的前几秒

最近项目上有一个需求是:根据一张图片的拍摄时间获取到这个时间前二后三的一个五秒钟的视频信息,通过查找相关资料写了一个方法拿来记录分享一下。

//指定时间减2秒 function reduceTwoS(dateStr){//dateStr格式为yyyy-mm-dd hh:mm:ss var dt=new Date(dateStr.replace(/-/,"https://www.jb51.net/"));//将传入的日期格式的字符串转换为date对象 兼容ie // var dt=new Date(dateStr);//将传入的日期格式的字符串转换为date对象 非ie var ndt=new Date(dt.getTime()-2000);//将转换之后的时间减去两秒 var result={ year:parseInt(ndt.getFullYear()), month:parseInt(ndt.getMonth()+1), day:parseInt(ndt.getDay()), hour:parseInt(ndt.getHours()), minute:parseInt(ndt.getMinutes()), second:parseInt(ndt.getSeconds()) } return result; } //指定时间加3秒 function addThreeS(dateStr){//dateStr格式为yyyy-mm-dd hh:mm:ss var dt=new Date(dateStr.replace(/-/,"https://www.jb51.net/"));//将传入的日期格式的字符串转换为date对象 兼容ie // var dt=new Date(dateStr);//将传入的日期格式的字符串转换为date对象 非ie var ndt=new Date(dt.getTime()+3000);//将转换之后的时间减去两秒 var result={ year:parseInt(ndt.getFullYear()), month:parseInt(ndt.getMonth()+1), day:parseInt(ndt.getDay()), hour:parseInt(ndt.getHours()), minute:parseInt(ndt.getMinutes()), second:parseInt(ndt.getSeconds()) } return result; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwydyx.html