date-format.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. [[mapping-date-format]]
  2. == Date Format
  3. In JSON documents, dates are represented as strings. Elasticsearch uses a set
  4. of pre-configured format to recognize and convert those, but you can change the
  5. defaults by specifying the `format` option when defining a `date` type, or by
  6. specifying `dynamic_date_formats` in the `root object` mapping (which will
  7. be used unless explicitly overridden by a `date` type). There are built in
  8. formats supported, as well as complete custom one.
  9. The parsing of dates uses http://www.joda.org/joda-time/[Joda]. The
  10. default date parsing used if no format is specified is
  11. http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateOptionalTimeParser--[ISODateTimeFormat.dateOptionalTimeParser].
  12. An extension to the format allow to define several formats using `||`
  13. separator. This allows to define less strict formats that can be used,
  14. for example, the `yyyy/MM/dd HH:mm:ss||yyyy/MM/dd` format will parse
  15. both `yyyy/MM/dd HH:mm:ss` and `yyyy/MM/dd`. The first format will also
  16. act as the one that converts back from milliseconds to a string
  17. representation.
  18. [float]
  19. [[date-math]]
  20. === Date Math
  21. The `date` type supports using date math expression when using it in a
  22. query/filter (mainly makes sense in `range` query/filter).
  23. The expression starts with an "anchor" date, which can be either `now`
  24. or a date string (in the applicable format) ending with `||`. It can
  25. then follow by a math expression, supporting `+`, `-` and `/`
  26. (rounding). The units supported are `y` (year), `M` (month), `w` (week),
  27. `d` (day), `h` (hour), `m` (minute), and `s` (second).
  28. Here are some samples: `now+1h`, `now+1h+1m`, `now+1h/d`,
  29. `2012-01-01||+1M/d`.
  30. When doing `range` type searches with rounding, the value parsed
  31. depends on whether the end of the range is inclusive or exclusive, and
  32. whether the beginning or end of the range. Rounding up moves to the
  33. last millisecond of the rounding scope, and rounding down to the
  34. first millisecond of the rounding scope. The semantics work as follows:
  35. * `gt` - round up, and use > that value (`2014-11-18||/M` becomes `2014-11-30T23:59:59.999`, ie excluding the entire month)
  36. * `gte` - round D down, and use >= that value (`2014-11-18||/M` becomes `2014-11-01`, ie including the entire month)
  37. * `lt` - round D down, and use < that value (`2014-11-18||/M` becomes `2014-11-01`, ie excluding the entire month)
  38. * `lte` - round D up, and use <= that value(`2014-11-18||/M` becomes `2014-11-30T23:59:59.999`, ie including the entire month)
  39. [float]
  40. [[built-in]]
  41. === Built In Formats
  42. Most of the below dates have a `strict` companion dates, which means, that
  43. year, month and day parts of the week must have prepending zeros in order
  44. to be valid. This means, that a date like `5/11/1` would not be valid, but
  45. you would need to specify the full date, which would be `2005/11/01` in this
  46. example. So instead of `date_optional_time` you would need to specify
  47. `strict_date_optional_time`.
  48. The following tables lists all the defaults ISO formats supported:
  49. [cols="<,<",options="header",]
  50. |=======================================================================
  51. |Name |Description
  52. |`basic_date`|A basic formatter for a full date as four digit year, two
  53. digit month of year, and two digit day of month (yyyyMMdd).
  54. |`basic_date_time`|A basic formatter that combines a basic date and time,
  55. separated by a 'T' (yyyyMMdd'T'HHmmss.SSSZ).
  56. |`basic_date_time_no_millis`|A basic formatter that combines a basic date
  57. and time without millis, separated by a 'T' (yyyyMMdd'T'HHmmssZ).
  58. |`basic_ordinal_date`|A formatter for a full ordinal date, using a four
  59. digit year and three digit dayOfYear (yyyyDDD).
  60. |`basic_ordinal_date_time`|A formatter for a full ordinal date and time,
  61. using a four digit year and three digit dayOfYear
  62. (yyyyDDD'T'HHmmss.SSSZ).
  63. |`basic_ordinal_date_time_no_millis`|A formatter for a full ordinal date
  64. and time without millis, using a four digit year and three digit
  65. dayOfYear (yyyyDDD'T'HHmmssZ).
  66. |`basic_time`|A basic formatter for a two digit hour of day, two digit
  67. minute of hour, two digit second of minute, three digit millis, and time
  68. zone offset (HHmmss.SSSZ).
  69. |`basic_time_no_millis`|A basic formatter for a two digit hour of day,
  70. two digit minute of hour, two digit second of minute, and time zone
  71. offset (HHmmssZ).
  72. |`basic_t_time`|A basic formatter for a two digit hour of day, two digit
  73. minute of hour, two digit second of minute, three digit millis, and time
  74. zone off set prefixed by 'T' ('T'HHmmss.SSSZ).
  75. |`basic_t_time_no_millis`|A basic formatter for a two digit hour of day,
  76. two digit minute of hour, two digit second of minute, and time zone
  77. offset prefixed by 'T' ('T'HHmmssZ).
  78. |`basic_week_date`|A basic formatter for a full date as four digit
  79. weekyear, two digit week of weekyear, and one digit day of week
  80. (xxxx'W'wwe). `strict_basic_week_date` is supported.
  81. |`basic_week_date_time`|A basic formatter that combines a basic weekyear
  82. date and time, separated by a 'T' (xxxx'W'wwe'T'HHmmss.SSSZ).
  83. `strict_basic_week_date_time` is supported.
  84. |`basic_week_date_time_no_millis`|A basic formatter that combines a basic
  85. weekyear date and time without millis, separated by a 'T'
  86. (xxxx'W'wwe'T'HHmmssZ). `strict_week_date_time` is supported.
  87. |`date`|A formatter for a full date as four digit year, two digit month
  88. of year, and two digit day of month (yyyy-MM-dd). `strict_date` is supported.
  89. _
  90. |`date_hour`|A formatter that combines a full date and two digit hour of
  91. day. strict_date_hour` is supported.
  92. |`date_hour_minute`|A formatter that combines a full date, two digit hour
  93. of day, and two digit minute of hour. strict_date_hour_minute` is supported.
  94. |`date_hour_minute_second`|A formatter that combines a full date, two
  95. digit hour of day, two digit minute of hour, and two digit second of
  96. minute. `strict_date_hour_minute_second` is supported.
  97. |`date_hour_minute_second_fraction`|A formatter that combines a full
  98. date, two digit hour of day, two digit minute of hour, two digit second
  99. of minute, and three digit fraction of second
  100. (yyyy-MM-dd'T'HH:mm:ss.SSS). `strict_date_hour_minute_second_fraction` is supported.
  101. |`date_hour_minute_second_millis`|A formatter that combines a full date,
  102. two digit hour of day, two digit minute of hour, two digit second of
  103. minute, and three digit fraction of second (yyyy-MM-dd'T'HH:mm:ss.SSS).
  104. `strict_date_hour_minute_second_millis` is supported.
  105. |`date_optional_time`|a generic ISO datetime parser where the date is
  106. mandatory and the time is optional. `strict_date_optional_time` is supported.
  107. |`date_time`|A formatter that combines a full date and time, separated by
  108. a 'T' (yyyy-MM-dd'T'HH:mm:ss.SSSZZ). `strict_date_time` is supported.
  109. |`date_time_no_millis`|A formatter that combines a full date and time
  110. without millis, separated by a 'T' (yyyy-MM-dd'T'HH:mm:ssZZ).
  111. `strict_date_time_no_millis` is supported.
  112. |`hour`|A formatter for a two digit hour of day. `strict_hour` is supported.
  113. |`hour_minute`|A formatter for a two digit hour of day and two digit
  114. minute of hour. `strict_hour_minute` is supported.
  115. |`hour_minute_second`|A formatter for a two digit hour of day, two digit
  116. minute of hour, and two digit second of minute.
  117. `strict_hour_minute_second` is supported.
  118. |`hour_minute_second_fraction`|A formatter for a two digit hour of day,
  119. two digit minute of hour, two digit second of minute, and three digit
  120. fraction of second (HH:mm:ss.SSS).
  121. `strict_hour_minute_second_fraction` is supported.
  122. |`hour_minute_second_millis`|A formatter for a two digit hour of day, two
  123. digit minute of hour, two digit second of minute, and three digit
  124. fraction of second (HH:mm:ss.SSS).
  125. `strict_hour_minute_second_millis` is supported.
  126. |`ordinal_date`|A formatter for a full ordinal date, using a four digit
  127. year and three digit dayOfYear (yyyy-DDD). `strict_ordinal_date` is supported.
  128. |`ordinal_date_time`|A formatter for a full ordinal date and time, using
  129. a four digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ss.SSSZZ).
  130. `strict_ordinal_date_time` is supported.
  131. |`ordinal_date_time_no_millis`|A formatter for a full ordinal date and
  132. time without millis, using a four digit year and three digit dayOfYear
  133. (yyyy-DDD'T'HH:mm:ssZZ).
  134. `strict_ordinal_date_time_no_millis` is supported.
  135. |`time`|A formatter for a two digit hour of day, two digit minute of
  136. hour, two digit second of minute, three digit fraction of second, and
  137. time zone offset (HH:mm:ss.SSSZZ). `strict_time` is supported.
  138. |`time_no_millis`|A formatter for a two digit hour of day, two digit
  139. minute of hour, two digit second of minute, and time zone offset
  140. (HH:mm:ssZZ). `strict_time_no_millis` is supported.
  141. |`t_time`|A formatter for a two digit hour of day, two digit minute of
  142. hour, two digit second of minute, three digit fraction of second, and
  143. time zone offset prefixed by 'T' ('T'HH:mm:ss.SSSZZ).
  144. `strict_t_time` is supported.
  145. |`t_time_no_millis`|A formatter for a two digit hour of day, two digit
  146. minute of hour, two digit second of minute, and time zone offset
  147. prefixed by 'T' ('T'HH:mm:ssZZ). `strict_t_time_no_millis` is supported.
  148. |`week_date`|A formatter for a full date as four digit weekyear, two
  149. digit week of weekyear, and one digit day of week (xxxx-'W'ww-e).
  150. `strict_week_date` is supported.
  151. |`week_date_time`|A formatter that combines a full weekyear date and
  152. time, separated by a 'T' (xxxx-'W'ww-e'T'HH:mm:ss.SSSZZ).
  153. `strict_week_date_time` is supported.
  154. |`week_date_time_no_millis`|A formatter that combines a full weekyear date
  155. and time without millis, separated by a 'T' (xxxx-'W'ww-e'T'HH:mm:ssZZ).
  156. `strict_week_date_time` is supported.
  157. |`weekyear`|A formatter for a four digit weekyear. `strict_week_year` is supported.
  158. |`weekyear_week`|A formatter for a four digit weekyear and two digit week
  159. of weekyear. `strict_weekyear_week` is supported.
  160. |`weekyear_week_day`|A formatter for a four digit weekyear, two digit week
  161. of weekyear, and one digit day of week. `strict_weekyear_week_day` is supported.
  162. |`year`|A formatter for a four digit year. `strict_year` is supported.
  163. |`year_month`|A formatter for a four digit year and two digit month of
  164. year. `strict_year_month` is supported.
  165. |`year_month_day`|A formatter for a four digit year, two digit month of
  166. year, and two digit day of month. `strict_year_month_day` is supported.
  167. |`epoch_second`|A formatter for the number of seconds since the epoch.
  168. Note, that this timestamp allows a max length of 10 chars, so dates
  169. older than 1653 and 2286 are not supported. You should use a different
  170. date formatter in that case.
  171. |`epoch_millis`|A formatter for the number of milliseconds since the epoch.
  172. Note, that this timestamp allows a max length of 13 chars, so dates
  173. older than 1653 and 2286 are not supported. You should use a different
  174. date formatter in that case.
  175. |=======================================================================
  176. [float]
  177. [[custom]]
  178. === Custom Format
  179. Allows for a completely customizable date format explained
  180. http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html[here].