---
title: "js 日期 当天 和 加上几天"
author: "bobshute"
date: "2009-05-12T17:56:20Z"
tags: []
slug: "js"
draft: false
lastmod: "2009-05-12T17:58:49Z"
id: 384404
url: "https://bobshute.iteye.com/blog/384404"
---

```
简单的显示第几天: 
```

```
 function showdate(n)
{
var uom = new Date();
uom.setDate(uom.getDate()+n);
uom = uom.getFullYear() + "-" +   (uom.getMonth()+1) + "-" + uom.getDate();
return uom;
}

window.alert("今天是："+showdate(0));
window.alert("昨天是："+showdate(-1));
window.alert("明天是："+showdate(1));
window.alert("10天前是："+showdate(-10));
window.alert("5天后是："+showdate(5)); 
```

 

```
返回格式化的日期,年份或日期不是2位则补位2位:如"2009-05-12"
```

```
function getOtherDate(n) {
	var nn = new Date();
	nn.setDate(nn.getDate()+n);
	year1 = nn.getYear();
	mon1 = nn.getMonth() + 1;
	date1 = nn.getDate();
	var monstr1;
	var datestr1
	if (mon1 < 10)
		monstr1 = "0" + mon1;
	else
		monstr1 = "" + mon1;

	if (date1 < 10)
		datestr1 = "0" + date1;
	else
		datestr1 = "" + date1;
	return year1 + "-" + monstr1 + "-" + datestr1;
}
```