Created
June 16, 2014 05:34
-
-
Save jakeauyeung/0bf24e6de7b6820ef547 to your computer and use it in GitHub Desktop.
根据指定的一个日期和相差的天数,获取另外一个日期,dateParameter为指定已经存在的日期yyyy-MM-dd num为相差天数为整型
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addByTransDate(dateParameter, num) { | |
var translateDate = "", | |
dateString = "", | |
monthString = "", | |
dayString = ""; | |
translateDate = dateParameter.replace("-", "/").replace("-", "/");; | |
var newDate = new Date(translateDate); | |
newDate = newDate.valueOf(); | |
newDate = newDate + num * 24 * 60 * 60 * 1000; //备注 如果是往前计算日期则为减号 否则为加号 | |
newDate = new Date(newDate); | |
//如果月份长度少于2,则前加 0 补位 | |
if ((newDate.getMonth() + 1).toString().length == 1) { | |
monthString = 0 + "" + (newDate.getMonth() + 1).toString(); | |
} else { | |
monthString = (newDate.getMonth() + 1).toString(); | |
} | |
//如果天数长度少于2,则前加 0 补位 | |
if (newDate.getDate().toString().length == 1) { | |
dayString = 0 + "" + newDate.getDate().toString(); | |
} else { | |
dayString = newDate.getDate().toString(); | |
} | |
dateString = newDate.getFullYear() + "-" + monthString + "-" + dayString; | |
return dateString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment