最近項目上有一個需求是:根據一張圖片的拍攝時間獲取到這個時間前二后三的一個五秒鍾的視頻信息,通過查找相關資料寫了一個方法拿來記錄分享一下。
//指定時間減2秒
function reduceTwoS(dateStr){//dateStr格式為yyyy-mm-dd hh:mm:ss
var dt=new Date(dateStr.replace(/-/,"/"));//將傳入的日期格式的字符串轉換為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.getDate()),
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(/-/,"/"));//將傳入的日期格式的字符串轉換為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.getDate()),
hour:parseInt(ndt.getHours()),
minute:parseInt(ndt.getMinutes()),
second:parseInt(ndt.getSeconds())
}
return result;
}