DATETIME_DIFF() missing a day : Why ? and How to? [not quite Solved]


#1

Hi everybody :slight_smile: !

I working on a simple DATETIME_DIFF() formula but the results I get seems off by on day and I really don’t understand why :confused: … Thus, I don’t seem to be able to correct that :confused:

The current situation is :

  • I’ve got a {Date} roll-up field simply pulling out a date with a MAX(value) from another table of my base.
  • This {Date} field format is set up as local and include a 24h time field.

What I simply want to do is to have in a formula field the numbers of days since that {Date}.

I used this formula to get to that result :

DATETIME_DIFF(TODAY(),{Date},'days')

… which works except for a missing day


(The 1st number is my DATETIME_DIFF() formula, the 2nd, the {Date} roll-up field)

As of NOW(), where I stand, we are the 9th June 2018 (9/6/2018), so the formula should give me an 8 for the 1/6/2018 and a 2 for the 7/6/2018, am I correct ?

My big question is :
How do I correct this ?

Do I need to change the formula in the roll-up field ? (maybe adding a SET_TIMEZONE() somewhere ?)
Is it because of the time field ? or the general formatting of my roll-up field ?
Change TODAY() in the formula field by a NOW(), TONOW()?
:thinking:

I’ve searched the community and found some threads reporting this as a bug, but I couldn’t found a solution or a work around…

As often, I feel I’m missing something simple :thinking:

Many big thank you in advance for the help :wink: !


#2

Sorry, I spoke too soon :sweat_smile:

I apparently found the solution to my own problem :sweat_smile: :yum: !

I won’t delete my post just in case someone is confronted to the same problem (who knows ? :wink: )

The problematic detail was, apparently, coming from the time field included in my {Date} roll-up field.

Replacing TODAY()by NOW() in the formula did the trick without a SET_TIMEZONE() (probably because my roll-up field is formatted as “local”)

:sweat_smile:


#3

I’ve run into other situations pulling date fields into formulas and spitting out a new date where the date was off by a day, and I wasnt using TODAY() or NOW(). The only thing that would fix it in my cases was to set the formula date field to “Use GMT for all collaborators” - which turned out ok, because I was always using relative dates without times and they came out right.

Definitely some unexpected behavior with dates sometimes in Airtable though.


#4

IIRC, TODAY() equates to midnight today (that is, the moment today began), while NOW() equates to the time when the function was called — and both are given in GMT.¹ Accordingly, if swapping TODAY() for NOW() (or vice-versa) ‘fixes’ your problem, it’s probably because the Δ between midnight GMT and the current time in GMT just happens to have been enough to skew the result correctly. Unfortunately, this means at other times of the day the problem is likely to reappear.
. __________

  1. This can lead to, um, non-intuitive results. For instance, my timezone is GMT -7, which means if I define a field with the formula TODAY() and configure field formatting (a) not to include a time field and (b) not to use GMT for all collaborators (that is, to use local time), TODAY() will always be yesterday, as midnight GMT is 5 p.m. the day before in my timezone.NOW() is yesterday until 7 a.m., when it becomes today — but TODAY() will always be yesterday.

FROMNOW() not in chronological order
#5

@Jeremy_Oglesby and @W_Vann_Hall, thanks to the both of you for your replies and enlightenments :smile: which got me to test a little further my “solution” by sort of reproducing “the problem”.
I just change the {Date} rollup field by a simple {Date} date field equally configured as the rollup field, thus including a time field.

The problem seems to be in the time field (which I need for something else in this table) included in the date and maybe how Airtable manages the “double field” in one … Dates and time can be so tricky with tiny subtleties! And I never really used an included time field before on Airtable …

As far as I can say, after testing, this lead me to 3 possible solutions :

  1. Leaving my DATETIME_DIFF() formula like it actually is, using NOW() instead of TODAY()
    This not a “bad solution” but a very easy one, as the missing day is count when the hour included in the {Date} field is passed, now, where I am, within a 2 minutes delay.
    This doesn’t make my DATETIME_DIFF() formula entirely reliable, but I think I can work with this ;).

  2. I can duplicate my initial {Date} rollup field, not include the time field and use this duplicated rollup field solely for the DATETIME_DIFF() formula using TODAY() instead of NOW() as the missing day is then count “normally” at midnight :wink:
    It’s a bit “messy” but seems more accurate and I think I could work with that too :wink: .

  3. Still using my initial {Date} rollup field with the included time field, the solution would be to suppress the time field within the DATETIME_DIFF() formula, or at least “tell to” the formula to just use the date and leave the time field on the side.
    Well, for this one, I don’t know if it’s doable (I couldn’t do it :confused:. I’m still too limited with my “Airtable capabilities” but still learning gladly too :smile: ). I think, it would be the “ideal” solution, if I could do it :sweat_smile:.

Maybe I’ve got my nose a little bit too much into this, but I don’t see what else I could try to get back that missing day.


#6

I think I finally found it :smile: !

As far as I can say, based on the results I got in my DATETIME_DIFF() Test table, I think I got the right formula that solves my missing problem by using my N°3 “solution” :smile: !

I tried to play with DATETIME_FORMAT() and DATETIME_PARSE() first but the results were kind of strange (some were right, others… I don’t even know how the formula gave me NaN results for some records)

So finally, to get rid of the time field included in the {Date} field, I converted it into another “date” using a DATESTR() within the DATETIME_DIFF() formula.

DATETIME_DIFF(TODAY(),DATESTR({Date}),'days')

This seems to give me the exact amount of days without adding other fields to my table :smile:

:tada:


#7

Just a note :yum:

Building around my previous formula :

I discovered some few details concerning the GMT time, which I didn’t take care of for a question of simplicity :wink: … Even though it was mentioned earlier in this thread :sweat_smile: .

Now, that my formula is getting more and more complex as I’m working on it, before going further, I finally looked into that GMT Timezone, trying to correct it :wink: .

I’m currently at GMT+2 so, as @W_Vann_Hall mentioned it, midnight, occurred at 2 AM for me last night.
(I saw it with my own eyes, because I was still working on this passed 2 AM :yum: )

Setting up my TIMEZONE, my final formula is :

DATETIME_DIFF(
	DATETIME_FORMAT(
		SET_TIMEZONE(
			TODAY(),
			'Europe/Brussels'
			),
		'L'
	),
	DATETIME_FORMAT(
		SET_TIMEZONE(
			{Date},
			'Europe/Brussels'
			),
		'L'
	),
	'days'
)&' J.'

This time I think I’m finally finished with this :smile: :tada:

(If someone could confirm though, it would be great :yum: )

I hope this will be helpful to someone one day :wink: