아래의 HTML 및 Javascript 날짜 선택 도구 예제는 거의 모든 브라우저에서 작동합니다. 네이티브 html datetime-local 입력 유형을 사용하며 datetime-local이 브라우저에서 지원되지 않는 경우 polyfin로 Pikaday (시간에 따라)로 폴백합니다. 그것을 실행하려면 https://github.com/owenmead/Pikaday에서 Pikaday를 다운로드하고 pikaday.js와 pikaday.css 을 추출하여이 코드가 들어있는 파일과 같은 폴더에 놓아야합니다.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="pikaday.css"/>
<script src="pikaday.js"></script>
<script type="text/javascript">
function setDate(elementId, date) {
// "elementId" is the element id of the date field to be updated and will be initialized to "date"
// in the timezone the user is browsing from.
if (elementId.type == "datetime-local") {
// datetime-local is supported, set the initial date in ISO format and show it
elementId.value = date.getFullYear() + '-'+
('0'+(date.getMonth()+1)).slice(-2) + '-' +
('0'+date.getDate()).slice(-2) + 'T' +
('0'+date.getHours()).slice(-2) + ':' +
('0'+date.getMinutes()).slice(-2);
}
else {
// Note that even though we assign the datetime-local input type to time fields it will be read back as text unless
// datetime-local is actually supported.
new Pikaday({
field : elementId,
defaultDate : date,
setDefaultDate : true,
showTime : true,
minutesStep : 5,
splitTimeView : false,
hours24format : true,
position : 'bottom left'
});
}
}
function getDateAsString(elementId) {
var dateAsString;
if (elementId.type == "datetime-local") {
dateAsString = elementId.value;
}
else {
var val = elementId.value;
// Convert time to ISO format which is the same format as that returned by datetime-local
var month = val.substring(4,7);
if (month == "Jan") month = "01";
else if (month == "Feb") month = "02";
else if (month == "Mar") month = "03";
else if (month == "Apr") month = "04";
else if (month == "May") month = "05";
else if (month == "Jun") month = "06";
else if (month == "Jul") month = "07";
else if (month == "Aug") month = "08";
else if (month == "Sep") month = "09";
else if (month == "Oct") month = "10";
else if (month == "Nov") month = "11";
else if (month == "Dec") month = "12";
var hour = val.substring(16,18);
if (val.indexOf("PM") != -1) {
hour = parseInt(hour);
if (hour < 12) {
// Have seen the likes of 21:15 PM returned by pikaday on firefox, hence the hour check
hour = hour+12;
}
}
dateAsString = val.substring(11,15) + "-" + month + "-" + val.substring(8,10) + "T" + hour + ":" + val.substring(19,21);
}
return dateAsString;
}
function getDate(elementId) {
var ds = getDateAsString(elementId);
var date = new Date(ds.substring(0,4), ds.substring(5,7)-1, ds.substring(8,10), ds.substring(11,13), ds.substring(14,16), ds.substring(17,19));
return date;
}
</script>
</head>
<body onload="setDate(document.getElementById('demo'), new Date());">
<input type="datetime-local" id="demo"/>
<br/><br/>
<button type="button" onclick="alert(getDateAsString(document.getElementById('demo')))">Show date as string</button>
<br/><br/>
<button type="button" onclick="alert(getDate(document.getElementById('demo')))">Show date as date object</button>
</body>
</html>
작업 코드를 제공 할 수 있습니까? –
당신은 HTML과 CSS만으로는 그걸 성취 할 수 없습니다. js를 사용해야합니다. 바닐라 JS도 마찬가지입니다. –
datepicker는 브라우저마다 다릅니다. 나는 이것이 할 수 있다고 생각하지 않는다. – Huelfe