GSheets - Time between date and now() - date

Trying different things and combos, but can't get it right. Times and dates always get me.
I'm simply trying to compare now() and a date/time and show "in 2 days 6 hrs 30 mins" or "3 days 1 hr 20 mins ago" and I'm guessing someone has done this already?
So, let's say the date in column A is 2023-01-24 10:00 and now it is 2023-01-25 11:05, then Col B should say "1 day 1 hr 5 mins ago"
I've tried duration(), date/time formatting the cell, days(..), but I can't find something that works reliably.

Another thing you could try:
=INT(NOW()-A1)&" d "&TEXT(NOW()-A1,"h \hr m \min a\go")

=NOW()-A1 will actually calculate the difference between Now and that time, BUT consider that the time is updated only when you make a change, it's not a countdown second by second. You can add an updater per minute but not more than that. Go to File - Settings and set On change and every minute:
If you need a specific format like that you mentioned you should then do calculations to add them. If you only need days, hours, minutes and seconds, you can do the next checkings and concatenations:
=LAMBDA(dif,
IF(INT(dif),INT(dif)&" days ","")
&IF(HOUR(dif),HOUR(dif)&" hours ")
&IF(MINUTE(dif),MINUTE(dif)&" minutes ")
&IF(SECOND(dif),SECOND(dif)&" seconds")&" ago")
(Now()-A1)

please try:
=TRIM(LAMBDA(y,IF(NOW()-A1<1,"0 days",IF(y=1,y&" "&"day",y&" "&"days")))(DATEDIF(A1,NOW(),"D"))&" "&
LAMBDA(y,IF(y=1,y&" "&"hr",y&" "&"hrs"))(HOUR(NOW()-A1))&" "&
LAMBDA(y,IF(y=1,y&" "&"min",y&" "&"mins"))(MINUTE(NOW()-A1))&" ago")

try:
=INDEX(IF(ISDATE_STRICT(A2:A), TRIM(FLATTEN(QUERY(TRANSPOSE(
IFERROR(LAMBDA(a, LAMBDA(x, IF(x=0,,IF(x>1, x&a&"s", x&a)))
(DATEDIF(A2:A, NOW(), {"Y", "YM", "MD"})))({" year", " month", " day"}))),,9^9))), ))

Related

Jiffy difference in month

I use Jiffy for calculate the difference in between 2 date in month.
But I don't the good result.
For end = 2/4/2023 and start = 1/4/2022 I have 12 months.
For end = 1/4/2023 and start = 1/4/2022 I have 11 months (error: expected 12).
Thanks,
num month = Jiffy(end).diff(Jiffy(start), Units.MONTH);
Right now it is checking 12 at midnight to 12 at midnight of the end date which is 11 months 30 days. Now if the end date is one second greater than the correct date it should work. So a hackey solution is to add one day duration to the end date and check
end.add(duration(day:1))

Extract time from date time and find difference between 2 times

I am trying to convert EPOC time to date time and need to extract the time only from that
I am doing below
$min = $Time_Start | measure -Minimum
$max = $Time_End | measure -Maximum
[datetime]$oUNIXDatemin=(Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds($min.Minimum))
$oUNIXDatemin_1 = $oUNIXDatemin.ToString("HH:mm:ss")
[datetime]$oUNIXDatemax=(Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds($max.Maximum))
$oUNIXDatemax_1 = $oUNIXDatemax.ToString("HH:mm:ss")
Problem is while converting I am getting $oUNIXDatemin_1 and $oUNIXDatemax_1 value like
$oUNIXDatemin_1
12 October 2021 07:46:46
$oUNIXDatemax_1
12 October 2021 21:16:04
My EPOC values are
$min.Minimum
1634024806
$max.Maximum
1634073364
Please let me know what is wrong here. Need to find the difference in HH:mm:ss format.
In PowerShell, you'd usually use a format string. Subtracting two PowerShell datetimes returns a value of type Timespan, which is well-behaved over a span of more than 24 hours.
([datetime]"12 October 2021 21:16:04" - [datetime]"12 October 2021 07:46:46") -f "HH:mm:ss"
13:29:18
Be careful here. Both intervals (durations) and time (of day) have the same format, but different meanings. For example, it makes sense to multiply the interval "01:00:00" (1 hour) by 3 to get three hours; it doesn't make sense to multiply the time "01:00:00" (1 o'clock AM) by 3.
I'm sure the overall calculation can be simplified, but it's too early for me.

How to get last Sunday's date?

I need to show last Sunday's date in a cell for a weekly report that I'm creating on google sheets. I've been googling to find a solution and the closest I found is this:
=TODAY()+(7-WEEKDAY(TODAY(),3))
But this gives next Monday's date. Any idea how to modify this to show last Sunday's date? Alternately, do you have another way to solve this problem?
The formula you're looking for would be:
=DATE(YY, MM, DD) - (WEEKDAY(DATE(YY, MM, DD)) - 1) - 7
// OR
=TODAY() - (WEEKDAY(TODAY()) - 1) - 7
Depending on what you take to be "last Sunday," you can simplify/factor this:
If you take "last Sunday" to mean, "the Sunday which has most recently happened:"
=DATE(A2,B2,C2) - WEEKDAY(DATE(A2,B2,C2)) + 1
If you take "last Sunday" to mean, "the Sunday which took place during the previous week:"
=DATE(A4,B4,C4) - WEEKDAY(DATE(A4,B4,C4)) - 6
Working through it:
=TODAY() - (WEEKDAY(TODAY()) - 1) - 7
TODAY()
// get today's date, ie. 22/11/2019
WEEKDAY(TODAY())
// get the current day of the week, ie. 6 (friday)
-
// take the first date and subtract that to rewind through the week,
// ie. 16/11/2019 (last saturday, since saturday is 0)
- 1
// rewind back one less than the entire week
// ie. 17/11/2019 (this sunday)
- 7
// rewind back to the sunday before this
// sunday, ie. 10/11/2019
Hopefully this explanation explains what the numbers at the end are doing. + 1 takes you from the Saturday of last week to the Sunday of the current week (what I would call "this Sunday"), and - 6 takes you back through last week to what I would call "last Sunday."
See here:
try:
=ARRAYFORMULA(TO_DATE(FILTER(ROW(INDIRECT(VALUE(TODAY()-6)&":"&VALUE(TODAY()))),
TEXT(ROW(INDIRECT(VALUE(TODAY()-6)&":"&VALUE(TODAY()))), "ddd")="Sun")))
if today is Sunday and you want still the last Sunday then:
=ARRAYFORMULA(IF(TEXT(TODAY(), "ddd")="Sun", TODAY()-6,
TO_DATE(FILTER(ROW(INDIRECT(VALUE(TODAY()-6)&":"&VALUE(TODAY()))),
TEXT(ROW(INDIRECT(VALUE(TODAY()-6)&":"&VALUE(TODAY()))), "ddd")="Sun"))))
Using monday as 1 index, this will return the previous sunday or today if it is sunday
=if((7-WEEKDAY(today(),2))>0,today()-(7-(7-WEEKDAY(today(),2))),today()+(7-WEEKDAY(today(),2)))
One can select for other days of the week by changing the number "7" directly before "-WEEKDAY(today(),2)" in the three places that pattern exists.

Increase number each day

I need to create a variable in PowerShell that increases with one each day. I'm going to use this variable in an email subject to define the Day Number as part of a test schedule. e.g. "Test - Day 38", when the script runs the next day it must ready "Test - Day 39".
I obviously can't use the date and AddDays, because the count is not limited to the number of days in the month.
Here is the code, $days is the result
# when counting starts, the first day
$startDate = [datetime]'2014-01-12'
# elapsed days (+ 1 in order to start with "day 1")
$days = [int]((Get-Date) - $startDate).TotalDays + 1
# result string
"Test - Day $days"
This code outputs (today)
Test - Day 38
Here's what I propose (it does involve using the date cmdlets):
When the test first runs, store the runtime in a file.
For example:
if (!(Test-Path startTime.txt)) {
get-date | out-file startTime.txt
}
Each time the test runs subsequently, read in the first_runtime from the file.
Subtract the current date (using get-date) from first_runtime.
This will have a .Days member you can extract to retrieve the number of Days elapsed.
Days : 2
Hours : 0
...

Unix gettimeofday() - compatible algorithm for determining week within month?

If I've got a time_t value from gettimeofday() or compatible in a Unix environment (e.g., Linux, BSD), is there a compact algorithm available that would be able to tell me the corresponding week number within the month?
Ideally the return value would work in similar to the way %W behaves in strftime() , except giving the week within the month rather than the week within the year.
I think Java has a W formatting token that does something more or less like what I'm asking.
[Everything below written after answers were posted by David Nehme, Branan, and Sparr.]
I realized that to return this result in a similar way to %W, we want to count the number of Mondays that have occurred in the month so far. If that number is zero, then 0 should be returned.
Thanks to David Nehme and Branan in particular for their solutions which started things on the right track. The bit of code returning [using Branan's variable names] ((ts->mday - 1) / 7) tells the number of complete weeks that have occurred before the current day.
However, if we're counting the number of Mondays that have occurred so far, then we want to count the number of integral weeks, including today, then consider if the fractional week left over also contains any Mondays.
To figure out whether the fractional week left after taking out the whole weeks contains a Monday, we need to consider ts->mday % 7 and compare it to the day of the week, ts->wday. This is easy to see if you write out the combinations, but if we insure the day is not Sunday (wday > 0), then anytime ts->wday <= (ts->mday % 7) we need to increment the count of Mondays by 1. This comes from considering the number of days since the start of the month, and whether, based on the current day of the week within the the first fractional week, the fractional week contains a Monday.
So I would rewrite Branan's return statement as follows:
return (ts->tm_mday / 7) + ((ts->tm_wday > 0) && (ts->tm_wday <= (ts->tm_mday % 7)));
If you define the first week to be days 1-7 of the month, the second week days 8-14, ... then the following code will work.
int week_of_month( const time_t *my_time)
{
struct tm *timeinfo;
timeinfo =localtime(my_time);
return 1 + (timeinfo->tm_mday-1) / 7;
}
Assuming your first week is week 1:
int getWeekOfMonth()
{
time_t my_time;
struct tm *ts;
my_time = time(NULL);
ts = localtime(&my_time);
return ((ts->tm_mday -1) / 7) + 1;
}
For 0-index, drop the +1 in the return statement.
Consider this pseudo-code, since I am writing it in mostly C syntax but pretending I can borrow functionality from other languages (string->int assignment, string->time conversion). Adapt or expand for your language of choice.
int week_num_in_month(time_t timestamp) {
int first_weekday_of_month, day_of_month;
day_of_month = strftime(timestamp,"%d");
first_weekday_of_month = strftime(timefstr(strftime(timestamp,"%d/%m/01")),"%w");
return (day_of_month + first_weekday_of_month - 1 ) / 7 + 1;
}
Obviously I am assuming that you want to handle weeks of the month the way the standard time functions handle weeks of the year, as opposed to just days 1-7, 8-13, etc.