10

Why in Elasticsearch we have the 'Z' at the end of the date field?

For instance:

2016-05-16T00:00:00.000Z

What does it mean?

Is this something useful for anything?

Is it harmful?

Can I get rid of it?

What about joda time?

Filipe Miranda
  • 834
  • 14
  • 30

2 Answers2

17

What does it mean?

The 'Z' means UTC.
Reference:
https://www.w3.org/TR/NOTE-datetime

Try not to store local dates. If you want to find a good thread on dates and why you should use UTC check this thread.

Is this something useful for anything?

It is very useful, storing all dates in UTC allows for easy conversion of dates from locale to locale. It also allows for date comparison and date visualisations to be consistent.

Is it harmful?

No.

Can I get rid of it?

If you wish? I wouldn't recommend doing it... It's a bit like removing the currency field from a transaction. The transaction doesn't make any sense.

Another example with dates:

I rang my friend in Country X (+6 hours ahead) at 1pm December 24th 2016. For him it was 7pm. So we have two local date times.

One for me:

1pm December 24th 2016 in London

One for my friend:

7pm December 24th 2016 in Country X

If I delete the 'in ...' Part these two become become two instances of time 5 hours apart of each other. Which means we couldn't have possibly spoken on the phone? Right?

No, because they are the same instance in time, across two locales.

What about joda time?

What about it?...

I hope this helps.

Luke Girvin
  • 12,672
  • 8
  • 57
  • 79
Rhys Bradbury
  • 1,566
  • 11
  • 23
  • Why do you say to not store local dates? – Filipe Miranda May 10 '16 at 22:54
  • 1
    For example: if I have a field called birth_timestamp, and I store my birth_timestamp to a centralised DB then someone from China saves their data also, you have to work out the time difference for doing any sort of charting. It's generally the norm and bet practise to use UTC. – Rhys Bradbury May 10 '16 at 22:57
  • 1
    Have a good read of this: http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result – Rhys Bradbury May 10 '16 at 22:58
1

From RFC3339 about Date and Time on the Internet:

Numeric offsets are calculated as "local time minus UTC". So the equivalent time in UTC can be determined by subtracting the offset from the local time. For example, 18:50:00-04:00 is the same time as 22:50:00Z. (This example shows negative offsets handled by adding the absolute value of the offset.)

So, a date with Z at the end is the date&time in UTC. And it should be the equivalent of 2016-05-16T00:00:00.000Z-00:00.

The presence of the timezone offset or not is just a matter of the date format being used in Elasticsearch's field definition.

Andrei Stefan
  • 48,348
  • 5
  • 84
  • 82