format.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. [[mapping-date-format]]
  2. === `format`
  3. In JSON documents, dates are represented as strings. Elasticsearch uses a set
  4. of preconfigured formats to recognize and parse these strings into a long
  5. value representing _milliseconds-since-the-epoch_ in UTC.
  6. Besides the <<built-in-date-formats,built-in formats>>, your own
  7. <<custom-date-formats,custom formats>> can be specified using the familiar
  8. `yyyy/MM/dd` syntax:
  9. [source,console]
  10. --------------------------------------------------
  11. PUT my-index-000001
  12. {
  13. "mappings": {
  14. "properties": {
  15. "date": {
  16. "type": "date",
  17. "format": "yyyy-MM-dd"
  18. }
  19. }
  20. }
  21. }
  22. --------------------------------------------------
  23. Many APIs which support date values also support <<date-math,date math>>
  24. expressions, such as `now-1m/d` -- the current time, minus one month, rounded
  25. down to the nearest day.
  26. [[custom-date-formats]]
  27. ==== Custom date formats
  28. Completely customizable date formats are supported. The syntax for these is explained in
  29. https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/format/DateTimeFormatter.html[DateTimeFormatter docs].
  30. [[custom-date-format-locales]]
  31. ===== Differences in locale information between JDK versions
  32. There can be some differences in date formats between JDK versions and different locales. In particular,
  33. there can be differences in text strings used for textual date formats, and there can be differences
  34. in the results of week-date calculations.
  35. There can be differences in text strings used by the following field specifiers:
  36. * `B`, `E`, `G`, `O`, `a`, `v`, `z` of any length
  37. * `L`, `M`, `Q`, `q`, `c`, `e` of length 3 or greater
  38. * `Z` of length 4
  39. If the text format changes between Elasticsearch or JDK versions, it can cause significant problems
  40. with ingest, output, and re-indexing. It is recommended to always use numerical fields in custom date formats,
  41. which are not affected by locale information.
  42. There can also be differences in week-date calculations using the `Y`, `W`, and `w` field specifiers.
  43. The underlying data used to calculate week-dates can vary depending on the JDK version and locale;
  44. this can cause differences in the calculated week-date for the same calendar dates.
  45. It is recommended that the built-in week-date formats are used, which will always use ISO rules
  46. for calculating week-dates.
  47. In particular, there is a significant change in locale information between JDK releases 22 and 23.
  48. Elasticsearch will use the _COMPAT_ locale database when run on JDK 22 and before,
  49. and will use the _CLDR_ locale database when run on JDK 23 and above. This change can cause significant differences
  50. to the textual date formats accepted by Elasticsearch, and to calculated week-dates. If you are using
  51. affected specifiers, you may need to modify your ingest or output integration code to account
  52. for the differences between these two JDK versions.
  53. [[built-in-date-formats]]
  54. ==== Built In Formats
  55. Most of the below formats have a `strict` companion format, which means that
  56. year, month and day parts of the month must use respectively 4, 2 and 2 digits
  57. exactly, potentially prepending zeros. For instance a date like `5/11/1` would
  58. be considered invalid and would need to be rewritten to `2005/11/01` to be
  59. accepted by the date parser.
  60. To use them, you need to prepend `strict_` to the name of the date format, for
  61. instance `strict_date_optional_time` instead of `date_optional_time`.
  62. These strict date formats are especially useful when
  63. <<date-detection,date fields are dynamically mapped>> in order to make sure to
  64. not accidentally map irrelevant strings as dates.
  65. The following tables lists all the defaults ISO formats supported:
  66. `epoch_millis`::
  67. A formatter for the number of milliseconds since the epoch. Note, that
  68. this timestamp is subject to the limits of a Java `Long.MIN_VALUE` and
  69. `Long.MAX_VALUE`.
  70. `epoch_second`::
  71. A formatter for the number of seconds since the epoch. Note, that this
  72. timestamp is subject to the limits of a Java `Long.MIN_VALUE` and `Long.
  73. MAX_VALUE` divided by 1000 (the number of milliseconds in a second).
  74. [[strict-date-time]]`date_optional_time` or `strict_date_optional_time`::
  75. A generic ISO datetime parser, where the date must include the year at a minimum, and the time
  76. (separated by `T`), is optional.
  77. Examples: `yyyy-MM-dd'T'HH:mm:ss.SSSZ` or `yyyy-MM-dd`.
  78. NOTE: When using `date_optional_time`, the parsing is lenient and will attempt to parse
  79. numbers as a year (e.g. `292278994` will be parsed as a year). This can lead to unexpected results
  80. when paired with a numeric focused format like `epoch_second` and `epoch_millis`.
  81. It is recommended you use `strict_date_optional_time` when pairing with a numeric focused format.
  82. [[strict-date-time-nanos]]`strict_date_optional_time_nanos`::
  83. A generic ISO datetime parser, where the date must include the year at a minimum, and the time
  84. (separated by `T`), is optional. The fraction of a second
  85. part has a nanosecond resolution.
  86. Examples: `yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ` or `yyyy-MM-dd`.
  87. `basic_date`::
  88. A basic formatter for a full date as four digit year, two digit month of
  89. year, and two digit day of month: `yyyyMMdd`.
  90. `basic_date_time`::
  91. A basic formatter that combines a basic date and time, separated by a 'T':
  92. `yyyyMMdd'T'HHmmss.SSSZ`.
  93. `basic_date_time_no_millis`::
  94. A basic formatter that combines a basic date and time without millis,
  95. separated by a 'T': `yyyyMMdd'T'HHmmssZ`.
  96. `basic_ordinal_date`::
  97. A formatter for a full ordinal date, using a four digit year and three
  98. digit dayOfYear: `yyyyDDD`.
  99. `basic_ordinal_date_time`::
  100. A formatter for a full ordinal date and time, using a four digit year and
  101. three digit dayOfYear: `yyyyDDD'T'HHmmss.SSSZ`.
  102. `basic_ordinal_date_time_no_millis`::
  103. A formatter for a full ordinal date and time without millis, using a four
  104. digit year and three digit dayOfYear: `yyyyDDD'T'HHmmssZ`.
  105. `basic_time`::
  106. A basic formatter for a two digit hour of day, two digit minute of hour,
  107. two digit second of minute, three digit millis, and time zone offset:
  108. `HHmmss.SSSZ`.
  109. `basic_time_no_millis`::
  110. A basic formatter for a two digit hour of day, two digit minute of hour,
  111. two digit second of minute, and time zone offset: `HHmmssZ`.
  112. `basic_t_time`::
  113. A basic formatter for a two digit hour of day, two digit minute of hour,
  114. two digit second of minute, three digit millis, and time zone off set
  115. prefixed by 'T': `'T'HHmmss.SSSZ`.
  116. `basic_t_time_no_millis`::
  117. A basic formatter for a two digit hour of day, two digit minute of hour,
  118. two digit second of minute, and time zone offset prefixed by 'T':
  119. `'T'HHmmssZ`.
  120. `basic_week_date` or `strict_basic_week_date`::
  121. A basic formatter for a full date as four digit weekyear, two digit week
  122. of weekyear, and one digit day of week: `xxxx'W'wwe`.
  123. `basic_week_date_time` or `strict_basic_week_date_time`::
  124. A basic formatter that combines a basic weekyear date and time, separated
  125. by a 'T': `xxxx'W'wwe'T'HHmmss.SSSZ`.
  126. `basic_week_date_time_no_millis` or `strict_basic_week_date_time_no_millis`::
  127. A basic formatter that combines a basic weekyear date and time without
  128. millis, separated by a 'T': `xxxx'W'wwe'T'HHmmssZ`.
  129. `date` or `strict_date`::
  130. A formatter for a full date as four digit year, two digit month of year,
  131. and two digit day of month: `yyyy-MM-dd`.
  132. `date_hour` or `strict_date_hour`::
  133. A formatter that combines a full date and two digit hour of day:
  134. `yyyy-MM-dd'T'HH`.
  135. `date_hour_minute` or `strict_date_hour_minute`::
  136. A formatter that combines a full date, two digit hour of day, and two
  137. digit minute of hour: `yyyy-MM-dd'T'HH:mm`.
  138. `date_hour_minute_second` or `strict_date_hour_minute_second`::
  139. A formatter that combines a full date, two digit hour of day, two digit
  140. minute of hour, and two digit second of minute: `yyyy-MM-dd'T'HH:mm:ss`.
  141. `date_hour_minute_second_fraction` or `strict_date_hour_minute_second_fraction`::
  142. A formatter that combines a full date, two digit hour of day, two digit
  143. minute of hour, two digit second of minute, and three digit fraction of
  144. second: `yyyy-MM-dd'T'HH:mm:ss.SSS`.
  145. `date_hour_minute_second_millis` or `strict_date_hour_minute_second_millis`::
  146. A formatter that combines a full date, two digit hour of day, two digit
  147. minute of hour, two digit second of minute, and three digit fraction of
  148. second: `yyyy-MM-dd'T'HH:mm:ss.SSS`.
  149. `date_time` or `strict_date_time`::
  150. A formatter that combines a full date and time, separated by a 'T':
  151. `yyyy-MM-dd'T'HH:mm:ss.SSSZ`.
  152. `date_time_no_millis` or `strict_date_time_no_millis`::
  153. A formatter that combines a full date and time without millis, separated
  154. by a 'T': `yyyy-MM-dd'T'HH:mm:ssZ`.
  155. `hour` or `strict_hour`::
  156. A formatter for a two digit hour of day: `HH`
  157. `hour_minute` or `strict_hour_minute`::
  158. A formatter for a two digit hour of day and two digit minute of hour:
  159. `HH:mm`.
  160. `hour_minute_second` or `strict_hour_minute_second`::
  161. A formatter for a two digit hour of day, two digit minute of hour, and two
  162. digit second of minute: `HH:mm:ss`.
  163. `hour_minute_second_fraction` or `strict_hour_minute_second_fraction`::
  164. A formatter for a two digit hour of day, two digit minute of hour, two
  165. digit second of minute, and three digit fraction of second: `HH:mm:ss.SSS`.
  166. `hour_minute_second_millis` or `strict_hour_minute_second_millis`::
  167. A formatter for a two digit hour of day, two digit minute of hour, two
  168. digit second of minute, and three digit fraction of second: `HH:mm:ss.SSS`.
  169. `ordinal_date` or `strict_ordinal_date`::
  170. A formatter for a full ordinal date, using a four digit year and three
  171. digit dayOfYear: `yyyy-DDD`.
  172. `ordinal_date_time` or `strict_ordinal_date_time`::
  173. A formatter for a full ordinal date and time, using a four digit year and
  174. three digit dayOfYear: `yyyy-DDD'T'HH:mm:ss.SSSZ`.
  175. `ordinal_date_time_no_millis` or `strict_ordinal_date_time_no_millis`::
  176. A formatter for a full ordinal date and time without millis, using a four
  177. digit year and three digit dayOfYear: `yyyy-DDD'T'HH:mm:ssZ`.
  178. `time` or `strict_time`::
  179. A formatter for a two digit hour of day, two digit minute of hour, two
  180. digit second of minute, three digit fraction of second, and time zone
  181. offset: `HH:mm:ss.SSSZ`.
  182. `time_no_millis` or `strict_time_no_millis`::
  183. A formatter for a two digit hour of day, two digit minute of hour, two
  184. digit second of minute, and time zone offset: `HH:mm:ssZ`.
  185. `t_time` or `strict_t_time`::
  186. A formatter for a two digit hour of day, two digit minute of hour, two
  187. digit second of minute, three digit fraction of second, and time zone
  188. offset prefixed by 'T': `'T'HH:mm:ss.SSSZ`.
  189. `t_time_no_millis` or `strict_t_time_no_millis`::
  190. A formatter for a two digit hour of day, two digit minute of hour, two
  191. digit second of minute, and time zone offset prefixed by 'T': `'T'HH:mm:ssZ`.
  192. `week_date` or `strict_week_date`::
  193. A formatter for a full date as four digit weekyear, two digit week of
  194. weekyear, and one digit day of week: `YYYY-'W'ww-e`.
  195. This uses the ISO week-date definition.
  196. `week_date_time` or `strict_week_date_time`::
  197. A formatter that combines a full weekyear date and time, separated by a
  198. 'T': `YYYY-'W'ww-e'T'HH:mm:ss.SSSZ`.
  199. This uses the ISO week-date definition.
  200. `week_date_time_no_millis` or `strict_week_date_time_no_millis`::
  201. A formatter that combines a full weekyear date and time without millis,
  202. separated by a 'T': `YYYY-'W'ww-e'T'HH:mm:ssZ`.
  203. This uses the ISO week-date definition.
  204. `weekyear` or `strict_weekyear`::
  205. A formatter for a four digit weekyear: `YYYY`.
  206. This uses the ISO week-date definition.
  207. `weekyear_week` or `strict_weekyear_week`::
  208. A formatter for a four digit weekyear and two digit week of weekyear:
  209. `YYYY-'W'ww`.
  210. This uses the ISO week-date definition.
  211. `weekyear_week_day` or `strict_weekyear_week_day`::
  212. A formatter for a four digit weekyear, two digit week of weekyear, and one
  213. digit day of week: `YYYY-'W'ww-e`.
  214. This uses the ISO week-date definition.
  215. `year` or `strict_year`::
  216. A formatter for a four digit year: `yyyy`.
  217. `year_month` or `strict_year_month`::
  218. A formatter for a four digit year and two digit month of year: `yyyy-MM`.
  219. `year_month_day` or `strict_year_month_day`::
  220. A formatter for a four digit year, two digit month of year, and two digit
  221. day of month: `yyyy-MM-dd`.