How can I add minutes to a timestamp?
If Iโve got a column thatโs a Time, and a column thatโs a number, and I want to add the minute equivalent of the number entered/selectedโฆ how?
For the examples below, letโs assume that: TimeNow() = โ08:00:00โ, and [Log_Start_Mod_Minutes] = 1 (number type)
----- You cant add a number, for this adds the hour equivalent of what youโve entered.
-> TimeNow() + 1 = โ09:00:00โ (aka, plus one hour)
-Itโs valid: https://cl.ly/2a541ed1f97a
The result: https://cl.ly/9132c8444eff < plus one hour
You canโt divide the number entered/selected by 60 to get the decimal, because that truncates to 0.
-> TimeNow() + 1/60 = โ08:00:00โ (1/60 = 0 so no change)
-Itโs valid: https://cl.ly/2e9381a7cd04 | https://cl.ly/09c08ebe55ea
The result: https://cl.ly/04a64b77b121 | https://cl.ly/e73cd699ac0f < no change (for both)
You canโt concatenate together a duration, using the number entered as the minutes, and add that to the timestamp
-> TimeNow() + concatenate(โ000:โ, [Log_Start_Mod_Minutes], โ:00โ)
-Itโs NOT valid: https://cl.ly/771e462e644c
But you can hard code it
-> TimeNow() + โ000:01:00โ = โ08:01:00โ (aka, plus one minute)
-------------------------------------- I believe this is the culprit of my issue: (https://cl.ly/3051dc94dc07) <- there are only two data types allowed to operate with the Time type:
number and duration.
Can we get decimal added to this list?
Other than something like thatโฆ anyone got any suggestions on how I can add minutes to a timestamp?
Yeah man, itโs lame. I had to do it hardcore style.
Especially important when youโre ignoring seconds
@Grant_Stead Iโm not sure about lame, but itโs something that Iโd really like to get working.
Howโs your hardcore style work?
Itโs something crazy =IF( OR( MINUTE(TIME(NOW()) - โ00:00:00โ)=MINUTE(TIME([clock_priorclock_datetime_device]) - โ00:00:00โ), [clock_priorclock_datetime_device]>NOW() ),
DATETIME(CONCATENATE(TODAY()," โ,HOUR(TIME([clock_priorclock_datetime_device]) - โ00:00:00โ),โ:",MINUTE(TIME([clock_priorclock_datetime_device]) - โ00:00:00โ)+1,":00")),
NOW() )
So basically extract each element out (hour, minute, seconds), do your math, then build it back together.
@MultiTech_Visions Yep, I worked this super hard for a very long time, and that was the best/only wayโฆ
Since @Grant_Stead mentioned working hard to get times and dates to look the way he wanted, I thought Iโd share something I put a lot of time into recently.
The column that contains the timestamp in my app is [Dated].
An expression to produce something short like
8/31 18:39
is
=concatenate(MONTH([Dated]),"/",DAY([Dated])," ",number(left(concatenate(TIME([Time])),2)),left(right(concatenate(TIME([Time])),6),3))
To produce a longer, more user friendly date like
September 2, 2018 (Sunday)
I concatenated
=concatenate(ifs(MONTH([Dated])=1,โJanuaryโ,MONTH([Dated])=2,โFebruaryโ,MONTH([Dated])=3,โMarchโ,MONTH([Dated])=4,โAprilโ,MONTH([Dated])=5,โMayโ,MONTH([Dated])=6,โJuneโ,MONTH([Dated])=7,โJulyโ,MONTH([Dated])=8,โAugustโ,MONTH([Dated])=9,โSeptemberโ,MONTH([Dated])=10,โOctoberโ,MONTH([Dated])=11,โNovemberโ,MONTH([Dated])=12,โDecemberโ,)," โ,day([Dated]),โ, ",year([Dated]))
with
=concatenate([Text date]," (",ifs(WEEKDAY([Dated])=1,โSundayโ,WEEKDAY([Dated])=2,โMondayโ,WEEKDAY([Dated])=3,โTuesdayโ,WEEKDAY([Dated])=4,โWednesdayโ,WEEKDAY([Dated])=5,โThursdayโ,WEEKDAY([Dated])=6,โFridayโ,WEEKDAY([Dated])=7,โSaturdayโ),")")
It works but itโs not easy.
One advantage, though, of building date expression up like this from scratch is that the system works in any language. This part of my app is in English but if I wanted to do something similar in Japanese, it would be just as easy (or hard, depending on your perspective
).
One workaround is if you convert the minute number as duration in a virtual column like CONCATENATE(โ000:โ,[NUMBER],":00"), you can then calculate the time like [OriginalTime]+[VirtualDuration].
Another Wizardly solution! TYTY! Translate
Iโve been working on this topic today because Iโm interested in using USERTZOFFSET() to adjust the โChangeTimeStampโ values produced by devices. The โChangeTimeStampโ column depends on the location of the device. So, if you want to have all of your times adjusted to local time in, say, in New York, you need to use USERTZOFFSET() to record the time zone of the device. USERTZOFFSET() tells you how many minutes the devices time is from UTC. So, this number is usually bigger than 60. That led me to use the following formula to put the time in a format that could be added to or subtracted from the ChangeTimeStamp:
concatenate(
left(โ00โ,2-len(text(floor(abs([Minutes of adjustment])/60)))),
floor(abs([Minutes of adjustment])/60),
โ:โ,
left(โ00โ,2-len(text(mod(abs([Minutes of adjustment]),60)))),mod(abs([Minutes of adjustment]),60),
โ:00โ
)
This converts 90 minutes, for example, to โ01:30:00โ โ the duration format that can be added to or subtracted from a time stamp value.
In this example, [Minutes of adjustment] is a column with the USERTZOFFSET() value. I needed to use abs() to make the negative numbers positive values, otherwise I wouldnโt be able to use the result to compute the new time because the format would get messed up. I then set up an if() formula to add if the value was positive and subtract if it was negative.
Thanks @Steve! I think youโre telling me that the answer is in the TEXT() article but Iโm not seeing it there. The USERTZOFFSET() value produces fairly big numbers ("-540" for Japan) that are numbers of minutes and not in a date format. If thereโs a quick and easy way to convert โ-540โ to the duration โ09:00:00โ I wonder if you could help me out by showing me how to do it.
SorryโI just skimmed your post, saw you appeared to be trying to format a date, and went with that. My bad.
To compute the deviceโs โUTC adjustmentโ, a Duration value that can be added to an arbitrary local DateTime value to get the corresponding UTC DateTime value:
(UTCNOW() - NOW())
Then:
([ChangeTimestamp] + (UTCNOW() - NOW()))
or, if stored in column UTC Adjustment:
([ChangeTimestamp] + [UTC Adjustment])
Honestly, I canโt imagine a good use for USERTZOFFSET() since I realized we could easily compute the offset as a readily-usable Duration.
Thanks much, @Steve! I always appreciate your help and expertise.
I agree with you that a USERTZOFFSET() would be much more useful if it were produced in a readily-usable Duration.
I see now that your much simpler solution will probably work in my case and allow me to avoid USERTZOFFSET().
Iโll play around with these. My only concern about UTCNOW() is that Iโve read that it is recorded as the server time but I want the device time (for instances where the sync has be delayed, I want to know when the user tapped something, not when it got written):
I donโt believe thatโs accurate.
Indeed. My testing indicates that it isnโt true. I put my phone on airplane mode, used my app, then synced. The UTCNOW() times were those of my phone.
Hi again @Steve!
After having tested your (UTCNOW() - NOW()) alternative for USERTZOFFSET() I would concur that (UTCNOW() - NOW()) works perfectly and that USERTZOFFSET() causes more problems than it solves because it renders the difference between the deviceโs time and UTC time in minutes, rather than a ready-to-compute format.
In regard to the original topic of this thread (how to add minutes to a timestamp) Iโd like to offer the following expression as a solution:
concatenate(
if([Minutes]>0,"","-"),
left(โ00โ,2-len(text(floor(abs([Minutes])/60)))),
floor(abs([Minutes])/60),
โ:โ,
left(โ00โ,2-len(text(mod(abs([Minutes]),60)))),
mod(abs([Minutes]),60),
โ:00โ
)
Itโs a bit convoluted, but itโs the best I could come up with in AppSheet. It works with both positive and negative values and values significantly larger than 60, such as those produced by USERTZOFFSET(). On a Google spreadsheet, the following will work:
=time(0,540,0)
The only problem is that the minute value has to be absolute so one needs another way to switch times to negative when thatโs whatโs called for. Iโve done it with an IF() formula.
Thanks again for your help with this, @Steve! I hope this can be a reference for others who have to deal with similar issues.
User | Count |
---|---|
16 | |
10 | |
10 | |
8 | |
3 |