Very custom date format in momentjs
-
21-12-2019 - |
Question
I am working with kibana (elasticsearch dashboard) which allow to specify a date pattern to explain the index naming pattern.
For instance, default pattern is: [logstash-]YYYY-MM-DD.HH
I'd like to organize my index by block of hours, let's say by block of 4 hours. Indices would then be named logstash-2014-02-25.00
, logstash-2014-02-25.04
, logstash-2014-02-25.08
, …
Is there any way to get such format with momentjs ? I am dreaming of [logstash-]YYYY-MM-DD.{HH%4}
but the documentation does not explain such thing (how weird).
La solution
It seems impossible to do it with formatting primitives.
However, I found we can override some of the language-dependant formatter such as meridiem formatter. A solution would then be to insert somewhere (in kibana conf file for example):
moment.lang('fourHourblocks', {
meridiem: function(hour, minute, isLowercase) {
hourBlock = Math.floor(hour / 4).toString();
if (hourBlock.length < 2) hourBlock = "0" + hourBlock;
return hourBlock;
}
}
then I can set the pattern to [logstash-]YYYY-MM-DD.A
.
This is kind of hacky so I am still opened to other solutions