Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lolicsystem/229632 to your computer and use it in GitHub Desktop.
Save lolicsystem/229632 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>クロージャを使って、今日昨日明日の日付を表示</title>
</head>
<body>
<script type="text/javascript">
function date_string() {
var nt = new Date();
var now = nt.getTime();
return function (offset) {
var uYear, uMon, uDate;
nt.setTime(now + offset * 86400000);
uYear = ("00" + (nt.getYear() - 100).toString()).slice(-2);
uMon = ("00" + (nt.getMonth() + 1).toString()).slice(-2);
uDate = ("00" + nt.getDate() .toString()).slice(-2);
var uDate = uYear + uMon + uDate;
return uDate;
}
}
var dt = date_string();
alert(dt(0)); // 今日
alert(dt(-1)); // 昨日
alert(dt(1)); // 明日
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment