format.asciidoc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. "my_type": {
  15. "properties": {
  16. "date": {
  17. "type": "date",
  18. "format": "yyyy-MM-dd"
  19. }
  20. }
  21. }
  22. }
  23. }
  24. --------------------------------------------------
  25. // AUTOSENSE
  26. Many APIs which support date values also support <<date-math,date math>>
  27. expressions, such as `now-1m/d` -- the current time, minus one month, rounded
  28. down to the nearest day.
  29. TIP: The `format` setting must have the same setting for fields of the same
  30. name in the same index. Its value can be updated on existing fields using the
  31. <<indices-put-mapping,PUT mapping API>>.
  32. [[custom-date-formats]]
  33. ==== Custom date formats
  34. Completely customizable date formats are supported. The syntax for these is explained
  35. http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html[in the Joda docs].
  36. [[built-in-date-formats]]
  37. ==== Built In Formats
  38. Most of the below dates have a `strict` companion dates, which means, that
  39. year, month and day parts of the week must have prepending zeros in order
  40. to be valid. This means, that a date like `5/11/1` would not be valid, but
  41. you would need to specify the full date, which would be `2005/11/01` in this
  42. example. So instead of `date_optional_time` you would need to specify
  43. `strict_date_optional_time`.
  44. The following tables lists all the defaults ISO formats supported:
  45. `epoch_millis`::
  46. A formatter for the number of milliseconds since the epoch. Note, that
  47. this timestamp allows a max length of 13 chars, so dates older than 1653
  48. and 2286 are not supported. You should use a different date formatter in
  49. that case.
  50. `epoch_second`::
  51. A formatter for the number of seconds since the epoch. Note, that this
  52. timestamp allows a max length of 10 chars, so dates older than 1653 and
  53. 2286 are not supported. You should use a different date formatter in that
  54. case.
  55. [[strict-date-time]]`date_optional_time` or `strict_date_optional_time`::
  56. A generic ISO datetime parser where the date is mandatory and the time is
  57. optional.
  58. http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateOptionalTimeParser--[Full details here].
  59. `basic_date`::
  60. A basic formatter for a full date as four digit year, two digit month of
  61. year, and two digit day of month: `yyyyMMdd`.
  62. `basic_date_time`::
  63. A basic formatter that combines a basic date and time, separated by a 'T':
  64. `yyyyMMdd'T'HHmmss.SSSZ`.
  65. `basic_date_time_no_millis`::
  66. A basic formatter that combines a basic date and time without millis,
  67. separated by a 'T': `yyyyMMdd'T'HHmmssZ`.
  68. `basic_ordinal_date`::
  69. A formatter for a full ordinal date, using a four digit year and three
  70. digit dayOfYear: `yyyyDDD`.
  71. `basic_ordinal_date_time`::
  72. A formatter for a full ordinal date and time, using a four digit year and
  73. three digit dayOfYear: `yyyyDDD'T'HHmmss.SSSZ`.
  74. `basic_ordinal_date_time_no_millis`::
  75. A formatter for a full ordinal date and time without millis, using a four
  76. digit year and three digit dayOfYear: `yyyyDDD'T'HHmmssZ`.
  77. `basic_time`::
  78. A basic formatter for a two digit hour of day, two digit minute of hour,
  79. two digit second of minute, three digit millis, and time zone offset:
  80. `HHmmss.SSSZ`.
  81. `basic_time_no_millis`::
  82. A basic formatter for a two digit hour of day, two digit minute of hour,
  83. two digit second of minute, and time zone offset: `HHmmssZ`.
  84. `basic_t_time`::
  85. A basic formatter for a two digit hour of day, two digit minute of hour,
  86. two digit second of minute, three digit millis, and time zone off set
  87. prefixed by 'T': `'T'HHmmss.SSSZ`.
  88. `basic_t_time_no_millis`::
  89. A basic formatter for a two digit hour of day, two digit minute of hour,
  90. two digit second of minute, and time zone offset prefixed by 'T':
  91. `'T'HHmmssZ`.
  92. `basic_week_date` or `strict_basic_week_date`::
  93. A basic formatter for a full date as four digit weekyear, two digit week
  94. of weekyear, and one digit day of week: `xxxx'W'wwe`.
  95. `basic_week_date_time` or `strict_basic_week_date_time`::
  96. A basic formatter that combines a basic weekyear date and time, separated
  97. by a 'T': `xxxx'W'wwe'T'HHmmss.SSSZ`.
  98. `basic_week_date_time_no_millis` or `strict_basic_week_date_time_no_millis`::
  99. A basic formatter that combines a basic weekyear date and time without
  100. millis, separated by a 'T': `xxxx'W'wwe'T'HHmmssZ`.
  101. `date` or `strict_date`::
  102. A formatter for a full date as four digit year, two digit month of year,
  103. and two digit day of month: `yyyy-MM-dd`.
  104. `date_hour` or `strict_date_hour`::
  105. A formatter that combines a full date and two digit hour of day.
  106. `date_hour_minute` or `strict_date_hour_minute`::
  107. A formatter that combines a full date, two digit hour of day, and two
  108. digit minute of hour.
  109. `date_hour_minute_second` or `strict_date_hour_minute_second`::
  110. A formatter that combines a full date, two digit hour of day, two digit
  111. minute of hour, and two digit second of minute.
  112. `date_hour_minute_second_fraction` or `strict_date_hour_minute_second_fraction`::
  113. A formatter that combines a full date, two digit hour of day, two digit
  114. minute of hour, two digit second of minute, and three digit fraction of
  115. second: `yyyy-MM-dd'T'HH:mm:ss.SSS`.
  116. `date_hour_minute_second_millis` or `strict_date_hour_minute_second_millis`::
  117. A formatter that combines a full date, two digit hour of day, two digit
  118. minute of hour, two digit second of minute, and three digit fraction of
  119. second: `yyyy-MM-dd'T'HH:mm:ss.SSS`.
  120. `date_time` or `strict_date_time`::
  121. A formatter that combines a full date and time, separated by a 'T': `yyyy-
  122. MM-dd'T'HH:mm:ss.SSSZZ`.
  123. `date_time_no_millis` or `strict_date_time_no_millis`::
  124. A formatter that combines a full date and time without millis, separated
  125. by a 'T': `yyyy-MM-dd'T'HH:mm:ssZZ`.
  126. `hour` or `strict_hour`::
  127. A formatter for a two digit hour of day.
  128. `hour_minute` or `strict_hour_minute`::
  129. A formatter for a two digit hour of day and two digit minute of hour.
  130. `hour_minute_second` or `strict_hour_minute_second`::
  131. A formatter for a two digit hour of day, two digit minute of hour, and two
  132. digit second of minute.
  133. `hour_minute_second_fraction` or `strict_hour_minute_second_fraction`::
  134. A formatter for a two digit hour of day, two digit minute of hour, two
  135. digit second of minute, and three digit fraction of second: `HH:mm:ss.SSS`.
  136. `hour_minute_second_millis` or `strict_hour_minute_second_millis`::
  137. A formatter for a two digit hour of day, two digit minute of hour, two
  138. digit second of minute, and three digit fraction of second: `HH:mm:ss.SSS`.
  139. `ordinal_date` or `strict_ordinal_date`::
  140. A formatter for a full ordinal date, using a four digit year and three
  141. digit dayOfYear: `yyyy-DDD`.
  142. `ordinal_date_time` or `strict_ordinal_date_time`::
  143. A formatter for a full ordinal date and time, using a four digit year and
  144. three digit dayOfYear: `yyyy-DDD'T'HH:mm:ss.SSSZZ`.
  145. `ordinal_date_time_no_millis` or `strict_ordinal_date_time_no_millis`::
  146. A formatter for a full ordinal date and time without millis, using a four
  147. digit year and three digit dayOfYear: `yyyy-DDD'T'HH:mm:ssZZ`.
  148. `time` or `strict_time`::
  149. A formatter for a two digit hour of day, two digit minute of hour, two
  150. digit second of minute, three digit fraction of second, and time zone
  151. offset: `HH:mm:ss.SSSZZ`.
  152. `time_no_millis` or `strict_time_no_millis`::
  153. A formatter for a two digit hour of day, two digit minute of hour, two
  154. digit second of minute, and time zone offset: `HH:mm:ssZZ`.
  155. `t_time` or `strict_t_time`::
  156. A formatter for a two digit hour of day, two digit minute of hour, two
  157. digit second of minute, three digit fraction of second, and time zone
  158. offset prefixed by 'T': `'T'HH:mm:ss.SSSZZ`.
  159. `t_time_no_millis` or `strict_t_time_no_millis`::
  160. A formatter for a two digit hour of day, two digit minute of hour, two
  161. digit second of minute, and time zone offset prefixed by 'T': `'T'HH:mm:ssZZ`.
  162. `week_date` or `strict_week_date`::
  163. A formatter for a full date as four digit weekyear, two digit week of
  164. weekyear, and one digit day of week: `xxxx-'W'ww-e`.
  165. `week_date_time` or `strict_week_date_time`::
  166. A formatter that combines a full weekyear date and time, separated by a
  167. 'T': `xxxx-'W'ww-e'T'HH:mm:ss.SSSZZ`.
  168. `week_date_time_no_millis` or `strict_week_date_time_no_millis`::
  169. A formatter that combines a full weekyear date and time without millis,
  170. separated by a 'T': `xxxx-'W'ww-e'T'HH:mm:ssZZ`.
  171. `weekyear` or `strict_weekyear`::
  172. A formatter for a four digit weekyear.
  173. `weekyear_week` or `strict_weekyear_week`::
  174. A formatter for a four digit weekyear and two digit week of weekyear.
  175. `weekyear_week_day` or `strict_weekyear_week_day`::
  176. A formatter for a four digit weekyear, two digit week of weekyear, and one
  177. digit day of week.
  178. `year` or `strict_year`::
  179. A formatter for a four digit year.
  180. `year_month` or `strict_year_month`::
  181. A formatter for a four digit year and two digit month of year.
  182. `year_month_day` or `strict_year_month_day`::
  183. A formatter for a four digit year, two digit month of year, and two digit
  184. day of month.