Preserving date format in Google Sheets in nested IFS statement - date

I have a Sheet with nested IFs, so that I can display different concatenated results depending on a value that I type in a certain cell.
Example:
A1 - I can type "Dog" "Cat" or "Rabbit"
Nested if says:
If "Dog" is in A1, then in cell A2, return G2&D2&F2
It's more complex, but that's the idea. So, formula essentially looks like this:
=IFS($A$1="Dog",G2&D2&F2,$A$1="Cat",C2&F2,R2,$A$1="Rabbit",H2,M2,N2)
Assume in the above that F2 contains dates in the format MM DD YY (i.e. 12/01/20).
If I call for F2 alone, it preserves the date format.
If I call F2 with other values (as above in the Dog and Cat examples), I get back a serial text string instead of the date-formatted value.
I want the date formatting of F2 to be preserved within Dog and Cat. How can I incorporate this into my IF statements and preserve the date formatting?

Found a thread from which I was able to deduce the answer. Here is the correct solution:
Instead of merely retrieving &F2, I substituted the following:
&TEXT(F2,"DDD, m/dd")
DDD, m/dd was the format I chose for the particular results I was seeking.
This returns "Mon, 8/3" for example.
Thanks!

Related

How do I convert Stata dates (%td format e.g. 30jan2015) into YYYYMMDD format (e.g. 20150130)

* date is in %td format
gen date1 = real(string(mofd(daily(date, "DMY")), "%tmCYN"))
* type mismatch error
tostring date, gen(dt)
gen date1 = real(string(mofd(daily(dt, "DMY")), "%tmCYN"))
* the code runs but generates no results
tostring date, gen(dt)
gen date2=date(dt, "YMD")
* the code runs but generates no results
If a date variable has a display format %td it must be numeric and stored as some kind of integer. The display format is, and is only, an instruction to Stata on how to display such integers. Confusions about conversion often seem to hinge on a misunderstanding about what format means, as format is an overloaded word in computing, referring variously to file format (as in graphics file format, .png or jpg or whatever); data layout (as in wide or long layout, structure or format); variable or storage type; and (here) display format. There could well be yet other meanings.
A date displayed as 30jan2015 is stored as an integer, namely
. display mdy(1, 30, 2015)
20118
and a glance at help data types shows that your variable date could be stored as an int, float, long or double. All would work, although int is least demanding of memory. You would need (e.g.) to run describe date to find out which type is being used in your case, but nothing to come in this answer depends on knowing that type. Note that finding out what Stata is doing and thinking can be illuminated by running display with simple, single examples.
Your question is ambiguous.
Want to change display format? If you wish merely to see your dates in a display format exemplified by 20150130 then consulting help datetime display formats shows that the display format is as tested here with display, which can be abbreviated all the way down to di
. di %tdCCYYNNDD 20118
20150130
so
format date %tdCCYYNNDD
is what you need. That instructs Stata to change the display format, but the numbers stored remain precisely as they were.
Want such dates as variables held as integers? If you want the dates to be held as integers like 20150130 then you could convert it to string using the display format above, and then to a real value. A minimal sandbox dataset shows this:
. clear
. set obs 1
Number of observations (_N) was 0, now 1.
. gen date = 20118
. gen wanted = real(strofreal(date, "%tdCCYYNNDD"))
. format wanted %8.0f
. l
+------------------+
| date wanted |
|------------------|
1. | 20118 20150130 |
+------------------+
A display format such as %8.0f is needed to see such values directly.
Another method is to generate a large integer directly. You need to be explicit about a suitable storage type and (as just mentioned) need to set an appropriate format, but it can be got to work:
. gen long also = 10000 * year(date) + 100 * month(date) + day(date)
. format also %8.0f
Want such dates as variables held as strings? This is the previous solution, but leave off the real(). The default display format will work fine.
. gen WANTED = strofreal(date, "%tdCCYYNNDD")
. l
+-----------------------------+
| date wanted WANTED |
|-----------------------------|
1. | 20118 20150130 20150130 |
+-----------------------------+
I have not used tostring here but as its original author I have no bias against it. The principles needed here are better illustrated using the underlying function strofreal(). The older name string() will still work.
Turning to your code,
tostring date, gen(dt)
will just put integers like 20118 in string form, so "20118", but there is no way that Stata can understand that alone to be a daily date. You could have run tostring with a format argument, which would have been equivalent to the code above. The advantage of tostring would only be if you had several such variables you wished to convert at once, as tostring would loop over such variables for you.
I can't follow why you thought that conversion to a monthly date or use of a monthly date display format was needed or helpful, as at best you'd lose the information on day of the month. Thus at best Stata can only map a monthly date back to the first day of that month, and at worst a monthly date (here 660) could not be understood as anything you want.
. di mofd(20118)
660
. di %td mofd(20118)
22oct1961
. di %td dofm(mofd(20118))
01jan2015
There is no shortcut to understanding how Stata thinks about dates that doesn't involve reading the needed parts of help datetime and help datetime display formats.
Yet more explanation and examples can be found at https://www.stata-journal.com/article.html?article=dm0067

Getting the end date from start date and duration in PROLOG

I have just started to code in Prolog, and I cannot do what I would like to.
Basically I have 2 input: a start date "2018/02/14" and a duration "99 days/months". I would like to know if there would be a way to predict the end date from this, in PROLOG:
date(X,Y,Z):-is_a_date(X),is_a_duration(Y),...
The main problem is about date format...
Any tips ?
The main problem is about date format...
Prolog uses terms to model data. Terms can be atoms or numbers, or any compound terms obtained by applying functors or operators to other terms. / is an operator. So 2018/02/14 is a perfectly fine Prolog term:
?- Date = 2018/02/14.
Date = 2018/2/14.
This is different from most other programming languages, in which something like the above would be an expression that would be evaluated to a value. For example, entering 2018/2/14 into a Python prompt gives back 72.0714 because it thinks we want it to compute a number. But in Prolog the example above is not asking for a number; it simply says that 2018/02/14 is some data made up of three numbers separated by / signs.
To operate on Prolog data structures, we use unification:
?- Date = 2018/02/14, Date = Year/Month/Day.
Date = 2018/2/14,
Year = 2018,
Month = 2,
Day = 14.
We unified the date with another, similar term Year/Month/Day. Here, the leaves of the term are variables. Unification will bind these variables to the numbers in the date terms at the corresponding positions. That is all!
So (using meaningful variable and predicate names, which are very important in Prolog) you can write your predicate as:
startdate_duration_enddate(Year/Month/Day, Duration, EndDate) :-
... .

Looking for a way to calculate time lapse in openrefine

This is the given expression of GREL language on OpenRefine.
diff(date d1, date d2, optional string timeUnit)
For dates, returns the difference in given time units.
So the question is how to get the access to the values of both columns, that is not clear on presented on the documentation.
Thanks
The formula for accessing another column is:
cells.YourColumnName.value
If your column name contains spaces or non-ascii characters :
cells['Your Column Name'].value
So, assuming your two columns are named "date1" and "date2", and you want the difference in days, the GREL formula is as follows :
diff(cells.date1.value, cells.date2.value, "days")
or
diff(cells['date1'].value, cells['date2'].value, "days")
I found a way myself here is the example of the working command, the GREL documentation is not that explicit treating this procedure.
Here is the commend I used, I multiplied the result by -1 to make it positive.
diff(cells["DATA_COMPRA"].value, cells["DATA_VENCIMENTO"].value, "days") * -1
Hope that helps, I my have to come back here sometimes to get this script again and again.

Checking the format of a string in Matlab

So I'm reading multiple text files in Matlab that have, in their first columns, a column of "times". These times are either in the format 'MM:SS.milliseconds' (sorry if that's not the proper way to express it) where for example the string '29:59.9' would be (29*60)+(59)+(.9) = 1799.9 seconds, or in the format of straight seconds.milliseconds, where '29.9' would mean 29.9 seconds. The format is the same for a single file, but varies across different files. Since I would like the times to be in the second format, I would like to check if the format of the strings match the first format. If it doesn't match, then convert it, otherwise, continue. The code below is my code to convert, so my question is how do I approach checking the format of the string? In otherwords, I need some condition for an if statement to check if the format is wrong.
%% Modify the textdata to convert time to seconds
timearray = textdata(2:end, 1);
if (timearray(1, 1) %{has format 'MM.SS.millisecond}%)
datev = datevec(timearray);
newtime = (datev(:, 5)*60) + (datev(:, 6));
elseif(timearray(1, 1) %{has format 'SS.millisecond}%)
newtime = timearray;
You can use regular expressions to help you out. Regular expressions are methods of specifying how to search for particular patterns in strings. As such, you want to find if a string follows the formats of either:
xx:xx.x
or:
xx.x
The regular expression syntax for each of these is defined as the following:
^[0-9]+:[0-9]+\.[0-9]+
^[0-9]+\.[0-9]+
Let's step through how each of these work.
For the first one, the ^[0-9]+ means that the string should start with any number (^[0-9]) and the + means that there should be at least one number. As such, 1, 2, ... 10, ... 20, ... etc. is valid syntax for this beginning. After the number should be separated by a :, followed by another sequence of numbers of at least one or more. After, there is a . that separates them, then this is followed by another sequence of numbers. Notice how I used \. to specify the . character. Using . by itself means that the character is a wildcard. This is obviously not what you want, so if you want to specify the actual . character, you need to prepend a \ to the ..
For the second one, it's almost the same as the first one. However, there is no : delimiter, and we only have the . to work with.
To invoke regular expressions, use the regexp command in MATLAB. It is done using:
ind = regexp(str, expression);
str represents the string you want to check, and expression is a regular expression that we talked about above. You need to make sure you encapsulate your expression using single quotes. The regular expression is taken in as a string. ind would this return the starting index of your string of where the match was found. As such, when we search for a particular format, ind should either be 1 indicating that we found this search at the beginning of the string, or it returns empty ([]) if it didn't find a match. Here's a reproducible example for you:
B = {'29:59.9', '29.9', '45:56.8', '24.5'};
for k = 1 : numel(B)
if (regexp(B{k}, '^[0-9]+:[0-9]+\.[0-9]+') == 1)
disp('I''m the first case!');
elseif (regexp(B{k}, '^[0-9]+\.[0-9]+') == 1)
disp('I''m the second case!');
end
end
As such, the code should print out I'm the first case! if it follows the format of the first case, and it should print I'm the second case! if it follows the format of the second case. As such, by running this code, we get:
I'm the first case!
I'm the second case!
I'm the first case!
I'm the second case!
Without knowing how your strings are formatted, I can't do the rest of it for you, but this should be a good start for you.

Date Comparsion not working in Access VBA

I am trying to compare two dates.
But the temp always returns true.
Can you explain where i am going wrong
temp = (Format(CDate("27-Aug-09"), "dd-mmm-yy") > Format(CDate("07-Jul-12"), "dd-mmm-yy"))
You're formatting the values according to dd-mmm-yy - which is actually the format they're in to start with. So you're just comparing the strings "27-Aug-09" and "07-Jul-12"... at which point "2" is later than "0", so the comparison finishes really quickly.
I suspect you can just get rid of the Format calls, to compare the dates:
temp = (CDate("27-Aug-09") > CDate("07-Jul-12"))
That's assuming that CDate can handle the input, of course. (I expect that part is fine.)
If you really want to compare strings, you need to convert the dates into a naturally sortable format, e.g. yyyy-mm-dd.