en.javascript.info/1-js/7-js-misc/1-class-instanceof/1-format-date-polymorphic/_js.view/solution.js
2015-04-01 19:08:41 +03:00

32 lines
No EOL
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function formatDate(date) {
if (typeof date == 'number') {
// перевести секунды в миллисекунды и преобразовать к Date
date = new Date(date * 1000);
} else if (typeof date == 'string') {
// строка в стандартном формате автоматически будет разобрана в дату
date = new Date(date);
} else if (Array.isArray(date)) {
date = new Date(date[0], date[1], date[2]);
}
// преобразования для поддержки полиморфизма завершены,
// теперь мы работаем с датой (форматируем её)
return date.toLocaleString("ru", {day: '2-digit', month: '2-digit', year: '2-digit'});
/*
// можно и вручную, если лень добавлят в старый IE поддержку локализации
var day = date.getDate();
if (day < 10) day = '0' + day;
var month = date.getMonth() + 1;
if (month < 10) month = '0' + month;
// взять 2 последние цифры года
var year = date.getFullYear() % 100;
if (year < 10) year = '0' + year;
var formattedDate = day + '.' + month + '.' + year;
return formattedDate;
*/
}