# 날짜시간요일

 

var weekstr='일월화수목금토'; // 요일 스트링
now=new Date(); // 현재시간 가져오기
year=now.getYear(); // 년도 가져오기
month=now.getMonth(); // 월 가져오기 (+1)
date=now.getDate(); // 날짜 가져오기
hour=now.getHours(); // 시간 가져오기
min=now.getMinutes(); // 분 가져오기
sec=now.getSeconds(); // 초 가져오기
mils=now.getMilliseconds(); // 밀리초 가져오기
wkday=now.getDay(); // 요일수치 가져오기 0=일, 1=월,...
document.write(year+'년 '+(month+1)+'월 '+date+'일('+weekstr.substr(wkday,1)+') ');
document,write(hour+'시 '+min+'분 '+sec+'초:'+mils+'ms'); // 스트링으로 결합하여 출력하기

 

 

 

# 월 또는 일을 원하는 값만큼 더하여 날짜를 계산해주는 스크립트입니다.

<html>


<SCRIPT LANGUAGE="JavaScript">
<!--
function addDay(yyyy, mm, dd, pDay) // 년, 월, 일, 계산할 일자 (년도는 반드시 4자리로 입력)
{
var oDate; // 리턴할 날짜 객체 선언

dd = dd*1 + pDay*1; // 날짜 계산

mm--; // 월은 0~11 이므로 하나 빼준다

oDate = new Date(yyyy, mm, dd) // 계산된 날짜 객체 생성 (객체에서 자동 계산)

return oDate;
}

function addMonth(yyyy, mm, dd, pMonth) // 년, 월, 일, 계산할 월 (년도는 반드시 4자리로 입력)
{
var cDate; // 계산에 사용할 날짜 객체 선언

var oDate; // 리턴할 날짜 객체 선언

var cYear, cMonth, cDay // 계산된 날짜값이 할당될 변수

mm = mm*1 + ((pMonth*1)-1); // 월은 0~11 이므로 하나 빼준다

cDate = new Date(yyyy, mm, dd) // 계산된 날짜 객체 생성 (객체에서 자동 계산)

cYear = cDate.getFullYear(); // 계산된 년도 할당

cMonth = cDate.getMonth(); // 계산된 월 할당

cDay = cDate.getDate(); // 계산된 일자 할당

oDate = (dd == cDay) ? cDate : new Date(cYear, cMonth, 0); // 넘어간 월의 첫쨋날 에서 하루를 뺀 날짜 객체를 생성한다.

return oDate;
}

function calcDate(f)
{

var cDate;
var y,m,d,i;
var frm = document.form1;

y = frm.yyyy.value;
m = frm.mm.value;
d = frm.dd.value;
i = frm.i.value;

if(f == 1 )
{
cDate = addDay(y, m, d, i)
}else{
cDate = addMonth(y, m, d, i)
}

frm.y.value = cDate.getFullYear();
frm.m.value = cDate.getMonth()+1;
frm.d.value = cDate.getDate();

}
//-->
</SCRIPT>
</head>
<body> 
<form method="post" name="form1">
<INPUT TYPE="text" NAME="yyyy" size=4 value="1999">년
<INPUT TYPE="text" NAME="mm" size=2 value="12">월
<INPUT TYPE="text" NAME="dd" size=2 value="31">일
<BR>
<INPUT TYPE="text" NAME="i" size=2 value="2">++<BR>
<input type="button" onclick="calcDate(1)" value="날짜 더하기"> 
<input type="button" onclick="calcDate(0)" value="월 더하기"><BR>
<hr>
<INPUT TYPE="text" NAME="y" size=4>년
<INPUT TYPE="text" NAME="m" size=2>월
<INPUT TYPE="text" NAME="d" size=2>일
</form>
</body>
</html> 

'html, script' 카테고리의 다른 글

for문 사용예제  (0) 2017.04.12
이벤트 옵션  (0) 2017.04.12
색상코드  (0) 2017.04.12
랜덤 random  (0) 2017.04.12
프레임페이지 따로 호출시 프레임페이지연결  (0) 2017.04.11




+ Recent posts