format.asciidoc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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,js]
  10. --------------------------------------------------
  11. PUT my_index
  12. {
  13. "mappings": {
  14. "properties": {
  15. "date": {
  16. "type": "date",
  17. "format": "yyyy-MM-dd"
  18. }
  19. }
  20. }
  21. }
  22. --------------------------------------------------
  23. // CONSOLE
  24. Many APIs which support date values also support <<date-math,date math>>
  25. expressions, such as `now-1m/d` -- the current time, minus one month, rounded
  26. down to the nearest day.
  27. [[custom-date-formats]]
  28. ==== Custom date formats
  29. Completely customizable date formats are supported. The syntax for these is explained
  30. https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html[DateTimeFormatter docs].
  31. [[built-in-date-formats]]
  32. ==== Built In Formats
  33. Most of the below formats have a `strict` companion format, which means that
  34. year, month and day parts of the week must use respectively 4, 2 and 2 digits
  35. exactly, potentially prepending zeros. For instance a date like `5/11/1` would
  36. be considered invalid and would need to be rewritten to `2005/11/01` to be
  37. accepted by the date parser.
  38. To use them, you need to prepend `strict_` to the name of the date format, for
  39. instance `strict_date_optional_time` instead of `date_optional_time`.
  40. These strict date formats are especially useful when
  41. <<date-detection,date fields are dynamically mapped>> in order to make sure to
  42. not accidentally map irrelevant strings as dates.
  43. The following tables lists all the defaults ISO formats supported:
  44. `epoch_millis`::
  45. A formatter for the number of milliseconds since the epoch. Note, that
  46. this timestamp is subject to the limits of a Java `Long.MIN_VALUE` and
  47. `Long.MAX_VALUE`.
  48. `epoch_second`::
  49. A formatter for the number of seconds since the epoch. Note, that this
  50. timestamp is subject to the limits of a Java `Long.MIN_VALUE` and `Long.
  51. MAX_VALUE` divided by 1000 (the number of milliseconds in a second).
  52. [[strict-date-time]]`date_optional_time` or `strict_date_optional_time`::
  53. A generic ISO datetime parser where the date is mandatory and the time is
  54. optional.
  55. `basic_date`::
  56. A basic formatter for a full date as four digit year, two digit month of
  57. year, and two digit day of month: `yyyyMMdd`.
  58. `basic_date_time`::
  59. A basic formatter that combines a basic date and time, separated by a 'T':
  60. `yyyyMMdd'T'HHmmss.SSSZ`.
  61. `basic_date_time_no_millis`::
  62. A basic formatter that combines a basic date and time without millis,
  63. separated by a 'T': `yyyyMMdd'T'HHmmssZ`.
  64. `basic_ordinal_date`::
  65. A formatter for a full ordinal date, using a four digit year and three
  66. digit dayOfYear: `yyyyDDD`.
  67. `basic_ordinal_date_time`::
  68. A formatter for a full ordinal date and time, using a four digit year and
  69. three digit dayOfYear: `yyyyDDD'T'HHmmss.SSSZ`.
  70. `basic_ordinal_date_time_no_millis`::
  71. A formatter for a full ordinal date and time without millis, using a four
  72. digit year and three digit dayOfYear: `yyyyDDD'T'HHmmssZ`.
  73. `basic_time`::
  74. A basic formatter for a two digit hour of day, two digit minute of hour,
  75. two digit second of minute, three digit millis, and time zone offset:
  76. `HHmmss.SSSZ`.
  77. `basic_time_no_millis`::
  78. A basic formatter for a two digit hour of day, two digit minute of hour,
  79. two digit second of minute, and time zone offset: `HHmmssZ`.
  80. `basic_t_time`::
  81. A basic formatter for a two digit hour of day, two digit minute of hour,
  82. two digit second of minute, three digit millis, and time zone off set
  83. prefixed by 'T': `'T'HHmmss.SSSZ`.
  84. `basic_t_time_no_millis`::
  85. A basic formatter for a two digit hour of day, two digit minute of hour,
  86. two digit second of minute, and time zone offset prefixed by 'T':
  87. `'T'HHmmssZ`.
  88. `basic_week_date` or `strict_basic_week_date`::
  89. A basic formatter for a full date as four digit weekyear, two digit week
  90. of weekyear, and one digit day of week: `xxxx'W'wwe`.
  91. `basic_week_date_time` or `strict_basic_week_date_time`::
  92. A basic formatter that combines a basic weekyear date and time, separated
  93. by a 'T': `xxxx'W'wwe'T'HHmmss.SSSZ`.
  94. `basic_week_date_time_no_millis` or `strict_basic_week_date_time_no_millis`::
  95. A basic formatter that combines a basic weekyear date and time without
  96. millis, separated by a 'T': `xxxx'W'wwe'T'HHmmssZ`.
  97. `date` or `strict_date`::
  98. A formatter for a full date as four digit year, two digit month of year,
  99. and two digit day of month: `yyyy-MM-dd`.
  100. `date_hour` or `strict_date_hour`::
  101. A formatter that combines a full date and two digit hour of day:
  102. `yyyy-MM-dd'T'HH`.
  103. `date_hour_minute` or `strict_date_hour_minute`::
  104. A formatter that combines a full date, two digit hour of day, and two
  105. digit minute of hour: `yyyy-MM-dd'T'HH:mm`.
  106. `date_hour_minute_second` or `strict_date_hour_minute_second`::
  107. A formatter that combines a full date, two digit hour of day, two digit
  108. minute of hour, and two digit second of minute: `yyyy-MM-dd'T'HH:mm:ss`.
  109. `date_hour_minute_second_fraction` or `strict_date_hour_minute_second_fraction`::
  110. A formatter that combines a full date, two digit hour of day, two digit
  111. minute of hour, two digit second of minute, and three digit fraction of
  112. second: `yyyy-MM-dd'T'HH:mm:ss.SSS`.
  113. `date_hour_minute_second_millis` or `strict_date_hour_minute_second_millis`::
  114. A formatter that combines a full date, two digit hour of day, two digit
  115. minute of hour, two digit second of minute, and three digit fraction of
  116. second: `yyyy-MM-dd'T'HH:mm:ss.SSS`.
  117. `date_time` or `strict_date_time`::
  118. A formatter that combines a full date and time, separated by a 'T':
  119. `yyyy-MM-dd'T'HH:mm:ss.SSSZZ`.
  120. `date_time_no_millis` or `strict_date_time_no_millis`::
  121. A formatter that combines a full date and time without millis, separated
  122. by a 'T': `yyyy-MM-dd'T'HH:mm:ssZZ`.
  123. `hour` or `strict_hour`::
  124. A formatter for a two digit hour of day: `HH`
  125. `hour_minute` or `strict_hour_minute`::
  126. A formatter for a two digit hour of day and two digit minute of hour:
  127. `HH:mm`.
  128. `hour_minute_second` or `strict_hour_minute_second`::
  129. A formatter for a two digit hour of day, two digit minute of hour, and two
  130. digit second of minute: `HH:mm:ss`.
  131. `hour_minute_second_fraction` or `strict_hour_minute_second_fraction`::
  132. A formatter for a two digit hour of day, two digit minute of hour, two
  133. digit second of minute, and three digit fraction of second: `HH:mm:ss.SSS`.
  134. `hour_minute_second_millis` or `strict_hour_minute_second_millis`::
  135. A formatter for a two digit hour of day, two digit minute of hour, two
  136. digit second of minute, and three digit fraction of second: `HH:mm:ss.SSS`.
  137. `ordinal_date` or `strict_ordinal_date`::
  138. A formatter for a full ordinal date, using a four digit year and three
  139. digit dayOfYear: `yyyy-DDD`.
  140. `ordinal_date_time` or `strict_ordinal_date_time`::
  141. A formatter for a full ordinal date and time, using a four digit year and
  142. three digit dayOfYear: `yyyy-DDD'T'HH:mm:ss.SSSZZ`.
  143. `ordinal_date_time_no_millis` or `strict_ordinal_date_time_no_millis`::
  144. A formatter for a full ordinal date and time without millis, using a four
  145. digit year and three digit dayOfYear: `yyyy-DDD'T'HH:mm:ssZZ`.
  146. `time` or `strict_time`::
  147. A formatter for a two digit hour of day, two digit minute of hour, two
  148. digit second of minute, three digit fraction of second, and time zone
  149. offset: `HH:mm:ss.SSSZZ`.
  150. `time_no_millis` or `strict_time_no_millis`::
  151. A formatter for a two digit hour of day, two digit minute of hour, two
  152. digit second of minute, and time zone offset: `HH:mm:ssZZ`.
  153. `t_time` or `strict_t_time`::
  154. A formatter for a two digit hour of day, two digit minute of hour, two
  155. digit second of minute, three digit fraction of second, and time zone
  156. offset prefixed by 'T': `'T'HH:mm:ss.SSSZZ`.
  157. `t_time_no_millis` or `strict_t_time_no_millis`::
  158. A formatter for a two digit hour of day, two digit minute of hour, two
  159. digit second of minute, and time zone offset prefixed by 'T': `'T'HH:mm:ssZZ`.
  160. `week_date` or `strict_week_date`::
  161. A formatter for a full date as four digit weekyear, two digit week of
  162. weekyear, and one digit day of week: `xxxx-'W'ww-e`.
  163. `week_date_time` or `strict_week_date_time`::
  164. A formatter that combines a full weekyear date and time, separated by a
  165. 'T': `xxxx-'W'ww-e'T'HH:mm:ss.SSSZZ`.
  166. `week_date_time_no_millis` or `strict_week_date_time_no_millis`::
  167. A formatter that combines a full weekyear date and time without millis,
  168. separated by a 'T': `xxxx-'W'ww-e'T'HH:mm:ssZZ`.
  169. `weekyear` or `strict_weekyear`::
  170. A formatter for a four digit weekyear: `xxxx`.
  171. `weekyear_week` or `strict_weekyear_week`::
  172. A formatter for a four digit weekyear and two digit week of weekyear:
  173. `xxxx-'W'ww`.
  174. `weekyear_week_day` or `strict_weekyear_week_day`::
  175. A formatter for a four digit weekyear, two digit week of weekyear, and one
  176. digit day of week: `xxxx-'W'ww-e`.
  177. `year` or `strict_year`::
  178. A formatter for a four digit year: `yyyy`.
  179. `year_month` or `strict_year_month`::
  180. A formatter for a four digit year and two digit month of year: `yyyy-MM`.
  181. `year_month_day` or `strict_year_month_day`::
  182. A formatter for a four digit year, two digit month of year, and two digit
  183. day of month: `yyyy-MM-dd`.