Skip to content

Instantly share code, notes, and snippets.

@pokatomnik
Created July 13, 2016 18:24
Show Gist options
  • Save pokatomnik/94621ab7810f71c60c0ec019bb04ae98 to your computer and use it in GitHub Desktop.
Save pokatomnik/94621ab7810f71c60c0ec019bb04ae98 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="ru">
<head>
<title>Format Date</title>
<meta charset="utf-8">
<style type="text/css">
input {
display : block;
}
</style>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
input = document.querySelector('input');
span = document.querySelector('span');
input.addEventListener('input', function () {
var output, inputPattern,
month, day, year,
hours, minutes,
str0, str1;
output = input.value,
input, span;;
inputPattern = /^(([1-9])|(0[1-9]{1})|(1[0-2]{1}))\.(([1-9]{1})|(0[1-9]{1})|(1[0-9]{1})|(2[0-9]{1})|(3[0-1]{1}))\.([0-9]{1,4})\ (([0-9]{1})|(1[0-9]{1})|(2[0-4]{1})|00)\:(([0-9]{1})|([0-5]{1}[0-9]{1}))$/;
correctFormat = output.match(inputPattern) ? true : false;
// Проверка на правильность для того чтобы пока пользователь вводит дату не парсить текст
if (correctFormat) {
str0 = output.split(' ')[0];
str1 = output.split(' ')[1];
month = str0.split('.')[0];
day = str0.split('.')[1];
year = str0.split('.')[2];
hours = str1.split(':')[0];
minutes = str1.split(':')[1];
span.innerText = hours+
'-'+
minutes+
' '+
day+
' '+
[
'January', 'Febriary', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
][parseInt(month)-1]+
' '+
year;
} else {
span.innerText = 'Некорректный ввод даты';
}
});
});
</script>
</head>
<body>
<input type="text">
<span>Введите дату</span>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment