Change Date/Time Format

Ex) 20130505 -> 2013-05-05

<script language="javascript">
function chkDate(obj) {
var input = obj.value.replace(/-/g,"");
var inputYear = input.substr(0,4);
var inputMonth = input.substr(4,2) - 1;
var inputDate = input.substr(6,2);
var resultDate = new Date(inputYear, inputMonth, inputDate);
if ( resultDate.getFullYear() != inputYear ||
resultDate.getMonth() != inputMonth ||
resultDate.getDate() != inputDate) {
obj.value = "";
} else {
obj.value = inputYear + "-" + input.substr(4,2) + "-" + inputDate;
}
}
</script>

Ex) 211001 -> 21:10:01

<script language="javascript">
function chkTime(obj) {
var input = obj.value.replace(/:/g,"");
var inputHours = input.substr(0,2);
var inputMinutes = input.substr(2,2);
var inputSeconds = input.substr(4,2);
var resultTime = new Date(0,0,0, inputHours, inputMinutes, inputSeconds);
if ( resultTime.getHours() != inputHours ||
resultTime.getMinutes() != inputMinutes ||
resultTime.getSeconds() != inputSeconds) {
obj.value = "";
} else {
obj.value = inputHours + ":" + inputMinutes + ":" + inputSeconds;
}
}
</script>

Posted in jQuery, Javascript