Why I keep geting my Date wrong on Google Sheets? - date

folks.
I am cleaning a dataset however in the Date row I get a strange number when I convert it do Date.
I have this:
And I get this:
What I wanted is dd-mm-yyyy.
What I tried:
I went to More Formats and manually separated the date, but it is still not recognised "Date".
I know this must be very basic, but I ave looked everywhere and couldnt find a solution.

the "date" in your D column is a string /plain text
you will need to convert it into date with helper column
=ARRAYFORMULA(IFERROR(DATE(LEFT(D2:D; 4); MID(D2:D; 5; 2); RIGHT(D2:D; 2))))

Related

extracting date from from string

I have following string:
CO_CAS_REA_NO_VIPPLUS_20190402_BONUS
I would like to Extract the Date part (20190402) and convert it into DD/MM/YY format if possible.
I have tried some 'Search' and 'extraction' formulas but to no avail.
does anyone have any ideas how to do this in excel or VBA?
Other variations of the data string would be like:
COCASREAHVPLUSVIPUK20190827CRMFSDEP
COCASREALVWINFIDEGL20190809CRMPTS
COSBINFLVFIDE20190830CRMBET
CO_CAS_RET_HVMV_UK_20190830_RB
COSB_REA_181INF_HVMV_FIDE_20190809_CRM_RB
COSBREAHVMVGL20190831CRMFBFSQUAL
COSBINFLVNO20190816CRMFB
Suppose in each string you only have one 8-digit numerical string representing the date in the form of YYYYMMDD that you want to extract, you can use one of the following three formulas to return the date:
Method 1:
=DATE(LEFT(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),4),MID(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),5,2),RIGHT(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),2))
Method 2:
=DATEVALUE(LEFT(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),4)&"/"&MID(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),5,2)&"/"&RIGHT(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),2))
Method 3:
=DATEVALUE(TEXT(MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)),"0000-00-00"))
Please note, all methods are using array formula so you MUST press Ctrl+Shift+Enter upon finishing the formula in the formula bar otherwise they will not function correctly. Then you can simply drag the formula down to apply across.
The logic is to use this array formula =MAX(IFERROR(--MID(A1,ROW($A$1:INDEX($A:$A,LEN(A1))),8),0)) to extract the numerical string first. Method 1 & 2 follow the same logic which is to extract the year YYYY, month MM and day DD component from the numerical string and then use use DATE or DATEVALUE function to return the date. Method 3 is using a formatting trick to quickly turn the numerical value into a text date and then use DATEVALUE function to convert the text date into a "real" date.
Let me know if you have any questions. Cheers :)
Assume your data put in Column A
In B2, enter formula :
=0+TEXT(AGGREGATE(14,6,--MID(A1,ROW($1:$250),8),1),"0000-00-00")
Then, B2 >> Format Cell, in the Number tab, choose: "Date" >> select dd/mm/yy
All copied down
with excel-formula, make use of MID() function
=MID("CO_CAS_REA_NO_VIPPLUS_20190402_BONUS",23, 8)
with VBA, you can split it with "_"
dte_str = Split("CO_CAS_REA_NO_VIPPLUS_20190402_BONUS", "_")(5)
You might find this more flexible if the text to the left changes:
Just to save you typing here is the formula in cell C1:
=MID(A1,FIND("LUS_",A1,1)+4,FIND("_BON",A1,1)-FIND("LUS_",A1,1)-4)
It does however assume that the 4 characters either side of the date are always the same.
Assuming the strings are in Column A and you're only looking for the year 2019 you could use this
=DATE(LEFT(MID(A1,SEARCH(2019,A1),8),4), MID(MID(A1,SEARCH(2019,A1),8),5,2), RIGHT(MID(A1,SEARCH(2019,A1),8),2))

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.

Google Sheets, 8 digit number to date format

I am using Google Sheets to import a list of names and dates from a comma delimited text file. It imports fine but when I try to convert the date cells I get strange results. The dates are 8 digits long and arranged "year, month, day". for example, i have this date. 20170425 when I format it to any of the date formats I get 57124-09-13.
I've tried everything I can think of and searched forever for an answer.
Thanks so much in advance to anyone who can steer me in the right direction!
Since you've added an Excel tag, use Data, Text to Columns, Fixed Width, Next, Date: YMD, Finish.
For google-sheets, use this formula in an unused column,
=DATEVALUE(replace(replace(A1, 7, 0, "/"), 5, 0, "/"))

Raw Excel Data contains different Date formats

I have huge amounts of raw data that are separated by columns. All is well when i import these to Matlab except for the fact that I just saw that the excel files contains different formats for the dates.
One series (i.e 3 days, 1 row or each hour gets 3x24 rows) have its' dates in the format "mm/dd/yyyy" which neither excel or matlab recognizes as proper dates.
I've tried solving this problem in different ways. First i tried to just highlight the cells and use the function format cells, but this didn't work since excel doesn't see them as 'cells' but rather as 'text'.
Then i tried the Text to columns function which didn't work either (delimited or fixed width).
Im really stuck and would appreciate some help with this.
In Excel:
If cell A1 has a string like mm/dd/yyyy then try this:
=DATE(RIGHT(A1,4), LEFT(A1,2), MID(A1,4,2))
In Matlab:
=datenum(yourDateString, 'mm/dd/yyyy')
Select the desired range to fix and use this script:
Sub bulk_Date_fix()
on error resume next
Set d_ranged = Selection
For Each a In d_ranged
a.Value = Split(a.Value, "/")(0) & "/" & Split(a.Value, "/")(1) & "/" & Split(a.Value, "/")(2)
Next
on error goto 0
End Sub
How it works: The above script loops through all the cells in the selected area and splits out the various attributes of a date based on the "/" symbol.
I examined your file and you will need to go back to the source data to straighten this out. Instead of "opening" the file in Excel, you will need to IMPORT the file. When you do that, the text import wizard will open, and you can then designate the date column as being of the format DMY (or whatever format is being generated by the source).
The problem is that there is a mismatch between the format of the file, and your Windows Regional Short date format. If you look at Row 229, you will see that real dates are present, but out of sequence with the rest.
Excel parses dates being input according to the Windows Regional Short Date settings. If the date doesn't make sense, according to that parsing (e.g. month > 12) , Excel will interpret the date as a string; if the date does make sense, it will be interpreted as a date in accordance with that windows regional date component order, and this will be different from what is intended.
So the first few hundred dates are strings, but at line 229, the date, which is probably meant to be 12 OCT 2014, gets changed to 10 DEC 2014. This will happen in other areas where that value in the 2nd position is 12 or less.
EDIT: I am not certain of the variabilities inherent in using XL on the MAC. In the Windows version of XL, the "text import" feature is on the Data Ribbon / Get External Data Tab:
When you click on that and open a text file, you will see the Text Import Wizard, and when you get to Step 3, you will be able to specify the text format of the data to be imported:

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.