In Google Sheets, subtract string "YYYY-MM-DD HH:MM:SS ET" from =now(). How to convert string to number? - date

We have the string 2022-06-01 11:01:05 ET in Google Sheets and we are looking to compute the difference between this string and =now(). We cannot simply subtract the cells because we cannot subtract a string from a number/date. How can we subtract and get the difference between timestamps (not just dates, precision to the hh:mm:ss would be useful.). The output we are looking for is a simple decimal number representing the time difference, that we can convert into minutes or seconds as needed.

try:
=(NOW()*1)-(1*REGEXEXTRACT(A1, "(.*) "))
then:
=TEXT((NOW()*1)-(1*REGEXEXTRACT(A1, "(.*) ")), "[h]:mm:ss")

See my comment to your original post. But as your post is written, you need to do this for only one cell. So assuming that cell is A2:
=NOW()-REGEXEXTRACT(A2,"(.+)\s\S+$")
... then set the format of that output to Duration.
Understand that since the NOW function is volatile (constantly changing), the output will also constantly be changing. Hopefully, that is what you want and expect.

You mention:
We have the string 2022-06-01 11:01:05 ET in Google Sheets...
It is safer to use:
=NOW()-REGEXEXTRACT(B1, "(.*) ")+(x/24)
(where x=difference in time zones and B1 is the cell containing the sting)
As an example, if your sheet's time zone is "(GMT+00:00) London", x should be adjusted accordingly
WHY?
Both of the answers provided by Erik as well as player0 will work if and only IF you are in the same time zone.
Considering the fact though that you do mention that the time zone in your question is a string (meaning fixed) as well as it is quite common for a Google Sheet to be in a different time zone, it is safer to use the above formula.
Please have a look at these times

Related

Change the Time in Date/Time Cells

I am trying to change the time in each Date/Time cell so that the time is 5:00:00. So that 05/10/2021 07:00:00 becomes 05/10/2021 05:00:00.
I have tried turning the cell into plain text, splitting it by the decimal and rejoining with the appropriate time, but it breaks my other formulas even though the format looks identical. I have also tried splitting the date and time, then using '''=CONCATENATE(text(A2,"M/D/YYYY")& " " &text($B$2,"H:MM:SS"))''' - the formulas still break.
if this is a valid DateTime cell just go into spreadsheet settings and change the timezone of your sheet to -2
update:
=INDEX(IF(A1:A="",,TEXT(INT(A1:A), "dd/mm/yyyy")&" 05:00:00"))

I want to write a formula in Google Sheets that counts the appearance of a value in a range, but the range is the result of another formula

I'm trying to count the number of times "Tuesday" (or any day of the week) appears in a range. However, my range is another formula, that is, the "Tuesday" is the result of a formula from another cell where I have put in the date.
For example, in B1, I have put in "2019.09.17." Thus, A1 will show, "Tuesday" because I set A1 to show the day of the week that corresponds to the date in B1.
If it matters, my formula in A1 is just "=B1" and then I changed the "Format>Number" to display just the day of the week.
My actual range is A1:A10.
I've tried =countif(A1:A10,"Tuesday"), but the result returns 0. I've also tried =countif(B1:B10,"Monday") to try and use the range from the "original" input, but the result is still 0.
Sorry if this question has been asked elsewhere and I'll be more than happy to rescind my question and read the answer elsewhere if someone could just direct me to it because I'm also actually not sure how to search for what I'm asking.
To get the "day of the week" you can use the following formula:
=TEXT(B1,"ddddd")
and then count normally "
Hope this helps!
Sidney
try like this:
=ARRAYFORMULA(COUNTIF(TO_TEXT(A1:A10), A12))

Google Sheets - Concatenate NOW-1, slash, and NOW+120

In Google Sheets, I need to create a date in the following format:
2016-06-15T12:00-0800/2016-10-16T12:00-0800
(Yesterday's date / today's date + 120 days)
Using =NOW()-1, I get yesterday's date.
Using Format - Date - More - Year(1930)-Month(05)-Day(01)T:Hour(01):00-0800, I get the proper format for the 1st part of the date range (2016-06-15T12:00-0800).
Repeating the same process with =NOW()+120.
Got the 2nd part of the date range (2016-10-16T12:00-0800).
PROBLEM: Trying to =CONCATENATE(A2,"/",B2), results in this:
42901.6965777315/43022.6965777315
...and no matter what I do - change the format, try to use =CONCATENATE(=TEXT(A2),"/",=TEXT(B2)), or other tricks I know, I either get a blank cell, an error message, or an even worse mess.
All I want is to combine 2 date cells into 1, with a slash in between. How can this be accomplished?
Try join instead of concatenate:
=join("/",A1,B1)
Maybe:
=text(now()-1,"yyyy-mm-ddThh:mm")&"-0800/"&text(now()+120,"yyyy-mm-ddThh:mm")&"-0800"
TEXT with only a date as argument returns the serial number corresponding to that date,
First you should confer the result of you formulas that involves NOW to a formatted text by using TEXT with the second argument. Then you could concatenate the result of that.
The above be donde on a single formula but you maybe should start by doing each step on separate cells in order to make it easy to check the result of each part.

Struggling with dates formats, want YYYY-MM-DD

As an absolute beginner to SAS I quickly ran into problems with date formatting.
I have a dataset containing transaction with three types of dates: BUSDATE, SPOTDATE, MATURITY. Each transaction is represented on two lines, and I want BUSDATE and SPOTDATE from line 1 but MATURITY from line 2.
In the original set, the dates are in YYYY-MM-DD format.
DATA masterdata;
SET sourcedata(rename(BUSDATE=BUSDATE2 SPOTDATE=SPOTDATE2 MATURITY=MATURITY2));
BUSDATE=BUSDATE2;
SPOTDATE=SPOTDATE2;
IF TRANS_TYPE='Swap' THEN;
MATURITY=SPOTDATE;
RUN;
Problem is, this returns something like 17169 (which I guess is the number of days from a certain date).
How can I make it output in YYYY-MM-DD format - or is this approach wrong; should I first convert the date variables to some SAS date format?
if you have valid SAS dates, just add a FORMAT statement to your DATA STEP.
Format busdate spotdate maturity yymmdd10. ;
SAS dates are numeric variables. They represent the number of days since 1/1/1960. You use a FORMAT to display dates.
Adding to CarolinaJay's answer, you normally want to keep them as numeric format, since you can do math (like "# of days since date X") with them. However, if for some reason you need a character variable, you can do this:
date_As_char=put(datevar,YYMMDD10.);
Incidentally, YYMMDD10 will actually give you YYYY-MM-DD, as you asked for; if you want a different separator, see http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000589916.htm (YYMMDDxw. format) - if you put a letter after the last D, for certain letters, you get a different separator. Like, YYMMDDn10. gives you no separator, or YYMMDDs10. gives you slashes. YYMMDDd10. gives you dashes, just like omitting the letter would. This concept also applies to MMDDYY formats, and I think a few others.

Bug in Zend_Date (back in time)

I have a very strange problem, Zend_Date is converting my timestamp to a year earlier.
In my action:
// Timestamp
$intTime = 1293922800;
// Zend_Date object
$objZendDate = new Zend_Date($intTime);
// Get date
echo date('Y-m-d',$intTime).'<br>';
echo $objZendDate->get('YYYY-MM-dd');
This outputs:
2011-01-02
2010-01-02
Can anyone tell me what i'm doing wrong?
From the ZF issue tracker it seems this is a known issue:
Recently a lot of ZF users are filing a bug that Zend_Date returns the wrong year, 2009 instead of 2008. This is however expected behaviour, and NOT A BUG!
From the FAQ:
When using own formats in your code you could come to a situation where you get for example 29.12.2009, but you expected to get 29.12.2008.
There is one year difference: 2009 instead of 2008. You should use the lower cased year constant. See this example:
$date->toString('dd.MM.yyyy');
instead of
$date->toString('dd.MM.YYYY');
From the manual
Note that the default ISO format differs from PHP's format which can be irritating if you have not used in previous. Especially the format specifiers for Year and Minute are often not used in the intended way.
For year there are two specifiers available which are often mistaken. The Y specifier for the ISO year and the y specifier for the real year. The difference is small but significant. Y calculates the ISO year, which is often used for calendar formats. See for example the 31. December 2007. The real year is 2007, but it is the first day of the first week in the week 1 of the year 2008. So, if you are using 'dd.MM.yyyy' you will get '31.December.2007' but if you use 'dd.MM.YYYY' you will get '31.December.2008'. As you see this is no bug but a expected behaviour depending on the used specifiers.
For minute the difference is not so big. ISO uses the specifier m for the minute, unlike PHP which uses i. So if you are getting no minute in your format check if you have used the right specifier.
To add to zwip's answer, what happens behind the scenes is that your date format YYYY-MM-dd is actually translated into o\-m\-d, which is then passed to PHP's date() function internally with the timestamp you provided.
Like mentioned in the other answer, and in the documentation for the o format on the date format page, the calculation of the year based on the ISO week can sometimes result in the year being one different to the value that you expect.