js實現字串格式的日期加一天
阿新 • • 發佈:2019-02-07
首先獲取字串格式的日期,然後獲取這個日期的時間戳,通過時間戳完成加一天的操作,再將時間戳轉換為date格式,再轉換為字串格式。
//增加一天 function addDate(time) { //加一天 var timestamp = Date.parse(new Date(time)); timestamp = timestamp /1000; timestamp += 86400;//加一天 var newTime =new Date(timestamp * 1000).format('yyyy-MM-dd hh:mm:ss'); return newTime; } //日期格式 Date.prototype.format = function(format) { var date = { "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+)/i.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)); } for (var k in date) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length)); } } return format; }