Excel: Check whether a cell has text in another cell and with symbol, or trim left and right - excel

So i have cell from long text from formula witch
=RIGHT(J2;15)
2016-07-22 2016
How to take out both 2016- and 2016 using find? It needs to be variable since in 2017 it wont be 2016. Can i find using something like FIND(K2&"-";J2)? Example below.
Result i need to get only 07
I used
=MID(LEFT(J2;FIND(N2;J2)-5);FIND(K2;J2)+5;LEN(J2))
But it finds both of 2016 and gives nothing
Wanted to something like
=MID(LEFT(J2;FIND(N2;J2)-5);FIND(K2&"-";J2)+5;LEN(J2))
But doesnt work

Pulling the left 10 characters off the text string should allow direct conversion to a true date using the DATEVALUE function (possibly assuming the conputer's correct system DMY vs. MDY regional setting). Once that true date has been achieved, a wrapping TEXT function can resolve any part of the date (e.g. year, month or day).
=TEXT(DATEVALUE(LEFT(J2, 10)), "mm") ◄ month as text representing a number
=--TEXT(DATEVALUE(LEFT(J2, 10)), "mm") ◄ month as true number

Related

What is the shortest formula in excel to covert text date format e.g. 17 Jan, 2023 into regular date format like 1/17/2023?

I have sales data where transaction date is mentioned as text like 17 Jan,2023. I want to extract month name using TEXT(A1,"MMM") or MONTH(A1) function however these two functions does not work with text date format.
I have tried multiple formulas to convert text date format to normal date format but I didn't get the right answer or the optimized small function or formula.
Later, I tried the following formula but it is too long and complex to use.
=DATE(RIGHT(A1,4),MATCH(MID(A1,1,3),{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"},0)-1+1,DAY(LEFT(A1,FIND(",",A1)-1)))
My question is that if anybody knows are shortest way to do the same?
In an English-language locale, the VALUE() function will recognise 17 Jan 2023 as a valid date (although you will have to render the date format manually), so the SUBSTITUTE() function can be used to remove the , that appear in your text, and then nesting this in the VALUE() function should return an Excel-recognised date, i.e.
=VALUE(SUBSTITUTE(A1,","," "))

Excel - Convert JavaScript/Unix timestamp to date

I'm wondering how to convert a timestamp to a date in Excel.
JavaScript timestamp:
1486812409161
Unix timestamp
1486812409
What Excel function / formula can convert to something like:
2/11/2017 11:26 AM (or any human readable date)
I did see this answer, but I can't get this to work for me (on Mac OS X / Excel 2011).
When I create a new cell and set it's value to the following formula:
= (MsValueCellReference / 86400000) + DATE(1970,1,1)
The result is: 41315.47696
I am using the current formula =(<javascriptTimestamp>)/(1000*60*60*24)+25569 and then formatting the cell with dd/mm/yyyy hh:mm:ss.
To explain the terms in the formula, the Javascript timestamp has milliseconds which accounts for the 1000. There are 60*60*24 seconds in one day.
Finally, Excel dates start on Jan 1 1900, and Javascript starts on Jan 1 1970. There are 25569 days between Jan 1 1900 and Jan 1 1970.
Excel is pretty happy to interconvert between dates and numbers, as you noticed. However, afaict, it can't always guess correctly which of the two you want to see.
So, to ensure that a value is rendered as a date, you'd need to open the Format Cells dialog, go to the first tab Number, and set the cell's format Category to one of the Date types.

Convert date e.g. Jun 05 2016 08:00:00 to dd/mm/yyyy hh:mm:ss

Got input such as in the topic title.
Trying to figure out how to convert this into UK date and time to be used in calculations.
I've looked at some methods on Google such as using text to columns, but I don't think this is what I'm looking for...
Thanks!
Edit: month is always in abbreviated format.
Edit 2: I should mention that I'm in the UK, and it doesn't seem to convert US date automagically.
Edit 3: Data:
Jun 05 2016 08:00:00 to dd/mm/yyyy hh:mm:ss
Assuming that your source date is a string and it is in the cell D10, the first thing you need to do is convert it to an excel date time serial. In the Excel Date Time serial there are a few things to note.
The integer portion of the number represents the number of days since Jan 1 1900 in windows and I think 1904 on mac
The decimal portion of the number represents the time in fraction of a day. 0.5 would represent noon. Valid Excel times for VBA are 00:00 to 23:59:59. 24:00 is not a valid time, though it will work with some excel formulas
So in order to convert your string to an Excel date serial we will need to rip out the components and dump them into the DATE() function. The date function consists of three arguments:
DATE(year,month,day)
Pretty straight forward with the exception that those values need to be numbers. Why dont we start pulling your information going from the largest unit to the smallest unit.
Thankfully your string is of consistent length. You have leading 0 for your single digits so they will occupy the same space as double digits. So this method will work until the year 9999, but I don't think we are too worried about that right now.
In order to pull the year we look at where it is in your string and how long it is. So by simply counting we know it starts in the 8th character position and its 4 characters long. We use this information with the MID() function
=MID(D10,8,4)
In order to pull the month it get a little more complicated since we need to convert it from an abbreviation to a number. There are several ways of doing this. You could go for a long IF statement which would wind up repeating the pull of the month a 11 times. Instead I decided to use the MATCH() function and built an array of month abbreviations inside it. The MATCH() function will return the number/position of what you are searching for in the provided search list. So as long as we enter the months in chronological order, their position will correspond to their numeric values. As such our formula will look like:
MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0)
The LEFT() function was used to pull the month abbreviation from your string. The 0 at the end tell match to look for an exact match. Important to note, this match method is not case sensitive.
Now to get the day we employ the same principals that we did for pulling the year and we wind up with:
=MID(D10,5,2)
We can now substitute each of the formulas for Year Month and Day into the DATE() function and we will get the date portion of the excel date serial. The formula should look like the following:
=DATE(MID(D10,8,4),MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0),MID(D10,5,2))
Now you need to tack on your time portion or figure out the decimal portion. In order to do this I would first recommend trying the TIMEVALUE() function. Since time formats tend to be a lot more standard in format than dates, there is a much higher probability that it will work for you. In order to use TIMEVALUE(), the time portion needs to be ripped from the string. This can easily be done with the RIGHT() function as follows:
=RIGHT(D10,8)
That will give you just the time portion which can then be substituted into the TIMEVALUE() function and looks like:
=TIMEVALUE(RIGHT(D10,8))
If the TIMEVALUE() function does not work for you, then you will need to strip out the hour minutes and seconds and dump their results in to the TIME() function. Do this in the same way you pulled the year and the day for the DATE() function. Just update your character counts. TIME() uses three arguments as follows:
TIME(HOUR,MINUTES,SECONDS)
Now that you have figured out your date portion and your time portion all you need to do is add them together to get all the information into one cell. The resulting formula will look like:
=DATE(MID(D10,8,4),MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0),MID(D10,5,2))+TIMEVALUE(RIGHT(D10,8))
Where ever you windup placing that formula, remember to change the formatting on the cell to a custom date. Enter the cell custom format as in the image below.
If you have a list of date times to convert in a column, simply copy your formula and formatted cell down as far as you need to go.
Proof of Concept
Formulas used
For more information on the functions used in the formulas above, follow the links below:
MATCH
DATE
TIMEVALUE
RIGHT
MID
LEFT
{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"}
The { } are used to build a custom list or static array.
If you don't want to write a macro in VBA, a cell formula would work if the format is as you specified:
I am assuming the data is in cell B3
=MID(B3,5,2) & "/" & IF(LEFT(B3,3)="Jan","01",IF(LEFT(B3,3)="Feb","02",IF(LEFT(B3,3)="Mar","03",IF(LEFT(B3,3)="Apr","04",IF(LEFT(B3,3)="May","05",IF(LEFT(B3,3)="Jun","06",IF(LEFT(B3,3)="Jul","07",IF(LEFT(B3,3)="Aug","08",IF(LEFT(B3,3)="Sep","09",IF(LEFT(B3,3)="Oct","10",IF(LEFT(B3,3)="Nov","11","12"))))))))))) & "/" & RIGHT(B3,13)
The DATEVALUE function expects a comma between the day and year; the REPLACE function can add that in. The TIMEVALUE function should be able to read that time without modification.
=DATEVALUE(REPLACE(LEFT(A1, 11), 7, 0, ","))+TIMEVALUE(RIGHT(A1, 8))
Note that the original in A1 is left-aligned; this indicates a text value. The converted date/time in B1 is right-aligned; this indicates a true date/time value.
Column B was formatted as dd/mm/yyyy hh:mm:ss. As General it would show as 42526.33333.

Convert long date text to date / time

I have an Excel (2010) spreadsheet delivered to me which is basically a text dump.
Cell - J8 has the following info Tue Feb 4 00:08:06 EST 2014
Cell - L8 has the following info Tue Feb 4 00:14:54 EST 2014
I need to calculate the difference in time between L8 and J8.
These cells are formatted as "text" and since they have the day, date, time and "EST" formatting the cells with long date it doesn't work.
I did this to parse the date/time value of each cell:
=DATE(RIGHT(J8,4),VLOOKUP(MID(J8,5,3),$P$2:$Q$13,2,FALSE),MID(J8,9,LEN(J8)-26))
+TIME(MID(J8,11+LEN(J8)-27,2),MID(J8,14+LEN(J8)-27,2),MID(J8,17+LEN(J8)-27,2))
Then you can just do a simple subtraction between the values of the two cells.
The only thing I needed to add to the spreadsheet was a lookup table to map month name to month value. Hence the VLOOKUP in the formula.
I also needed to calculate the offset position past the day value as it appears that it could have either one or two digits - hence the magic 26 and 27 numbers in the formula.
Uses an array (column of months MMM, and 1-12) and can surely be improved, but to tide you over:
=DATE(RIGHT(J8,4),VLOOKUP(MID(J8,5,3),array,2,0),MID(J8,9,2))+VALUE(MID(J8,11,9))
in say Y8 and copied across to Z8 with =Z8-Y8 in AA8 may suit (for the 'time' being!)
Does not cater for differences in time zones.
If L8 is always later than J8, and if the format is always as you show (with three letters for the timezone, and the spacing as above, then try:
=(MID(L8,5,6)&", "&RIGHT(L8,4) &" "& MID(L8,11,9))-(MID(J8,5,6)&", "&RIGHT(J8,4) &" "& MID(J8,11,9))
One further caveat with this method: your native default date (in Windows Control Panel, not in Excel itself) needs to be MDY as in your text dump.
EDIT: The following (longer) version should work no matter if your native date format is MDY or DMY:
=(MID(L8,9,2)& MID(L8,4,4)&RIGHT(L8,5)&" "&MID(L8,11,9))-(MID(J8,9,2)& MID(J8,4,4)&RIGHT(J8,5)&" "&MID(J8,11,9))

.NumberFormat sometimes returns the wrong value with dates and times

It seems that every week or so someone posts a question about dates being converted (corrupted?) to American format. Like many others, I have attempted to help but the problem is elusive. I now wonder if I have discovered the cause.
I am working on an application in which I need to extract data from an Excel worksheet and output it as strings formatted to match the value the Excel user can see. So if the value is “1” formatted to display as “1.00” then I want the string to be “1.00”.
I achieve this effect by testing the cell value to be a number, date or time. If it is, I retrieve the number format and use it to format the cell value so:
With .Cells(Row, Column)
Output string = Format(.Value, .NumberFormat)
End With
In most cases this gives me exactly the output I require. However, sometimes I get American dates and times when the source is formatted as a UK date or time.
After much experimentation with Excel 2003 and Excel 2007, I have discovered the cause. (I do not have access to Excel 2010 but from questions I deduce it has the same problem.) This question is in part intended to reveal this problem to the world because I can discover nothing on the internet to indicate that anyone else has noticed it. (No doubt someone will reply that they googled “xyz” and got the answer immediately.) However, the main purpose of this question is to seek suggestions for obtaining the result I need in all situations.
Typically I enter dates as, for example, “23mar12”. Excel recognises this as a date and formats it as “23-Mar-12”. I can select Format Cells and enter or select a custom format or select one of the date formats so I can have any format I can imagine wanting including non-English names for days and months.
However, in one case the format I select is not the format that is recorded: Custom format “dd/mm/yyyy” is recorded as Date format “* 14/03/2001”. This is not obviously a problem until further down the line.
I created a column of dates and times and formatted each with a different custom or standard format. I wrote a macro to extract the NumberFormat for each of these dates and times and write it as a string to an adjacent column. I also formatted the value using the number format and wrote that string to a third column.
In a number of cases the format selected and recorded by Excel was not the format returned by NumberFormat:
Excel format NumberFormat
Date: * 14/03/2001 m/d/yyyy
Date: * 14 March 2001 [$-F800]dddd, mmmm dd, yyyy
Date: 14/03/2001 dd/mm/yyyy;#
Date: 14/03/01 dd/mm/yy;#
Date: 14/3/01 d/m/yy;#
Date: 14.3.01 d.m.yy;#
Date: 2001-03-14 yyyy-mm-dd;#
Date: 14 March 2001 (1) [$-809]dd mmmm yyyy;#
Date: 14 March 2001 (2) [$-809]d mmmm yyyy;#
Custom: hh:mm:ss h:mm:ss
Time: * 13:30:55 [$-F400]h:mm:ss AM/PM
Time: 13:30:55 (1) hh:mm:ss;#
Time: 13:30:55 (2) h:mm:ss;#
Time: 01:30:55 PM [$-409]hh:mm:ss AM/PM;#
Time: 1:30:55 PM [$-409]h:mm:ss AM/PM;#
The values (1) and (2) in the Excel format column were added by me to indicate that there are two apparently identical formats. As can be seen from the NumberFormat column, in each case the second version suppresses a leading zero.
Most changes have no important effect. “[$-F800]” and so on are apparently dummy values with no effect. Apparently you can replace “F800” with an Microsoft country code to have the names of days and months translated to the language of that country.
However, the three standard formats that Microsoft marks with an asterisk are changed unacceptably. The dates are changed from little endian to middle endian; the time is changed from 24 hour to 12 hour and the day of the week has been added to “* 14 March 2001”.
The asterisk against the dates, references the comment: “Except for items that have an asterisk () in the Type list (Number tab, Format Cells dialog box), date formats that you apply do not switch date orders with the operating system.” The asterisk against the time, references the comment: “Except for items that have an asterisk () in the Type list (Number tab, Format Cells dialog box), time formats that you apply do not switch time orders with the operating system.”
If I have to, I can warn my users that standard date and time formats may not give the result desired. However, if they want the popular format “dd/mm/yyyy”, they cannot have it. “dd-mm-yyyy”, for example, is OK but custom format “dd/mm/yyyy” becomes date format “* 14/03/2001” becomes “m/d/yyyy”.
Returning to my opening point: is this strange handling of one particular date format the reason so many people claim their dates are sometimes being converted to American format and is this why the problem is so elusive? I have come across this type of problem elsewhere of one group of Microsoft programmers not knowing what another group are doing. Is this why some functions always work and other sometimes don’t? Some Microsoft programmers know where to look for the correct format and others don’t?
More importantly, for me, can anyone suggest:
How I obtain the true date or time format?
Some other way of determining the user’s chosen display format for a date or time?
BTW 1: I recall that thirty or so years ago I was told that the American military do not use month/day/year format; only American civilians use this format. Can anyone tell me if this is true?
BTW 2: The similar problem is with Excel colours. Excel holds its colours as "ggbbrr" while everybody else holds them as "rrggbb". The programmers for the .Net Excel inter-op were not told and and did not reverse the Excel colour number before using it to control the screen.
I have mainly come up against formatting and date issues when opening text files which have been saved with different regional settings. Two useful cell properties for dealing with this are:
.Text returns the cell value as it is displayed
.Value2 returns the unformatted cell value or date serial number.
As you say, standard date and number formats depend on windows regional settings and this may not be desired behavior as the same workbook can display differently in different regions. MS introduced the regional code prefixes in number formats (circa Excel 2000?) which enforce consistent display if needed but they need to be explicitly selected.
If you really want to see a date or number as the user entered it, you could extract the contents of the .xlsx file looking at the worksheet cell format and the shared strings xml definitions which list the number formats in the saved workbook. I don't really see a need to do this though as the underlying value is stored internally as a serial number and this will not change.
BTW 1: It's been almost 30 years since I was in the military...
I worked on helicopters and I was taught to use a format such as this in the aircraft logbooks: 3 Apr 12. So, that's how I still write dates. This way, there's no wondering about 4/3/2012 - is it April 3 or March 4?
I hacked this: I rewrite the original data in a known format. it relies on DateSerial and TimeSerial:
'Google spreadsheet stores dates in USA format (MM/DD/YYYY). We're in Australia, using DD/MM/YYYY, so we need to swap them.
'
With dc 'the cell who contains a date in USA format.
d = .Value 'capture value in USA format
t = TimeValue(d)
.NumberFormat = "dd/mm/yyyy" 'set to OZ format, so Excel knows the values were swapped in its internal math.
.Value = DateSerial(Year(d), Month(d), Day(d)) 'DateSerial takes y,d,m. We swap Month and Day components, to get OZ format dates
.Value = .Value + TimeSerial(Hour(t), Minute(t), Second(t))
dc.Font.Bold = True ' We bold the cells that are swapped, for debugging
End With
End If

Resources