Last active
December 20, 2015 10:20
-
-
Save ruslanchek/6115128 to your computer and use it in GitHub Desktop.
Очеловечивание времени - выводит дату в человеческом формате (27 июля, 2013, 21:00:15). Работает как со строками вида MySQL Timestamp, так и с простым объектом Date.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function humanizeDate(date, output_with_time) { | |
if(!(Object.prototype.toString.call(date) === "[object Date]")){ | |
var t = date.split(/[- :]/); | |
date = new Date(t[0], t[1] - 1, t[2], t[3], t[4], t[5]); | |
} | |
if (!date) { | |
return '—'; | |
} | |
var h_date, | |
month_names = [ | |
'января', | |
'февраля', | |
'марта', | |
'апреля', | |
'мая', | |
'июня', | |
'июля', | |
'августа', | |
'сентября', | |
'октября', | |
'ноября', | |
'декабря' | |
]; | |
var d = date.getDate(), | |
M = date.getMonth(), | |
Y = date.getFullYear(); | |
h_date = d + ' ' + month_names[M] + ', ' + Y; | |
if(output_with_time === true){ | |
var H = date.getHours(), | |
i = date.getMinutes(), | |
s = date.getSeconds(); | |
if(H < 10){ | |
H = '0' + H; | |
} | |
if(i < 10){ | |
i = '0' + i; | |
} | |
if(s < 10){ | |
s = '0' + s; | |
} | |
h_date = h_date + ', ' + H + ':' + i + ':' + s; | |
} | |
return h_date; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment