JS日期比较大小 给定时间和持续时间计算最终时间

<pre name="code" class="javascript">/**
	 * 往指定时间字符串上加时间间隔,获得新的时间字符串 
	 * startDateStr:开始时间字符串,类似"2015-7-20 17:26:00"
	 * durationNumStr:持续时间数
	 * durationTypeStr:持续时间类型
	 * 返回类似:2015-7-20 17:26:00
	 * 
	 * 
	 */
	function getEndDate(startDateStr, durationNumStr, durationTypeStr) {

		var startDate = new Date(startDateStr.replace("-", "/").replace("-",
				"/"));
		var duration_num = parseInt(durationNumStr);

		var endDate = "";
		if (durationTypeStr == "min") {
			endDate = new Date(startDate.setMinutes(startDate.getMinutes()
					+ duration_num));
		}
		if (durationTypeStr == "h") {
			endDate = new Date(startDate.setHours(startDate.getHours()
					+ duration_num));
		}
		if (durationTypeStr == "d") {
			endDate = new Date(startDate.setDate(startDate.getDate()
					+ duration_num));
		}
		if (durationTypeStr == "w") {
			endDate = new Date(startDate.setDate(startDate.getDate()
					+ duration_num * 7));
		}
		if (durationTypeStr == "m") {
			endDate = new Date(startDate.setMonth(startDate.getMonth()
					+ duration_num));
		}
		if (durationTypeStr == "y") {
			endDate = new Date(startDate.setFullYear(startDate.getFullYear()
					+ duration_num));
		}

		var year = endDate.getFullYear();
		var month = endDate.getMonth() + 1 < 10 ? "0"
				+ (endDate.getMonth() + 1) : endDate.getMonth() + 1;
		var date = endDate.getDate() < 10 ? "0" + endDate.getDate() : endDate
				.getDate();
		var hour = endDate.getHours() < 10 ? "0" + endDate.getHours() : endDate
				.getHours();
		var minute = endDate.getMinutes() < 10 ? "0" + endDate.getMinutes()
				: endDate.getMinutes();
		var second = endDate.getSeconds() < 10 ? "0" + endDate.getSeconds()
				: endDate.getSeconds();
		var endDateStr = year + "-" + month + "-" + date + " " + hour + ":"
				+ minute + ":" + second;
		return endDateStr;
	}

	/**
	 * 计算开始时间是否小于结束时间,小于等于返回true,否则false
	 * startDateStr:开始时间字符串
	 * endDateStr:结束时间字符串
	 * 返回:true或false
	 * 
	 * 
	 */
	function chkTime(startDateStr, endDateStr) {
		//计划截止时间
		var startDate = new Date(startDateStr.replace("-", "/").replace("-",
				"/"));
		//计划详细项的截止时间
		var endDate = new Date(endDateStr.replace("-", "/").replace("-", "/"));
		if (startDate <= endDate) {
			return true;
		} else {
			return false;
		}
	}


 
</pre><pre name="code" class="javascript">
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值