I have dates coming out of a database into a column that have this format:
Column K
AUG-14-2015 08:31:32 AM
AUG-12-2015 06:10:03 PM
AUG-12-2015 05:17:51 PM
I want to add like '2.2' days to each of these and put them in Column L. I have formatted the column in this format:
mmm-dd-yyyy hh:mm:ss AM/PM
I wasn't sure if the dashes are causing the problem, but when I do a =K15+2.2 I get a !VALUE. Any ideas what I need to do? Thanks.
After some experimenting, it seems that while you can specify a date format "mmm-dd-yyyy hh:mm:ss AM/PM", the actual entry into the cells must be either a date in the system wide locale specific format, or a number of days since 1 January 1900.
So for example when you set a cell's format to custom:
"mmm-dd-yyyy hh:mm:ss AM/PM"
In my case (Windows 10 default UK locale settings, Excel 2010) for AUG-12-2015 06:10:03 PM, I can enter any of:
12/08/2015 06:10:03 PM
12/08/2015 18:10:03
42228.26
And also exchange the month 08 for Aug or AUG in any case and change the '/' for '-' in the dates.
For cells formatted "mmm-dd-yyyy hh:mm:ss AM/PM", the output for any of the above inputs is:
Aug-12-2015 06:10:03 PM
Note that there does not appear to be a means to force all-capitals for the month.
Note also that if you enter a date/time in the locale format it is automatically recognised and formatted as date/time.
So really your only option is to either change the format of the source data, pre-process the data into locale date/time format, or write a macro to do it within the spreadsheet. It may also work perhaps if you change your locale settings (System Settings, Language & Time), but that is perhaps less than satisfactory because it will affect date presentation for the whole system.
If you want to avoid VBA, then you can add a column("L") next to your data and enter:
=SUBSTITUTE(K1,"-","/")
Another solution would be to write code to do just that to your existing "K" Column
Related
How do I modify the following to be D/M/YYYY?
8/7/20 6:20 PM
8/17/20 4:24 PM
Currently it is M/D/YY
Here is what I have done to no avail:
=VALUE(LEFT(A2,FIND(" ",A2,1)-1)) - This is to extract the date portion (because excel sees it as text)
Tried to convert the date to a D/M/Y using 'text to columns' but that does not work. Excel sees the middle number as the date so anything over 12 and it throws an error.
Thank for any help on this. It's breaking me.
use:
=TEXTJOIN("/",,FILTERXML("<a><b>"&SUBSTITUTE(LEFT(TEXT(A1,"d/m/yy")&" ",FIND(" ",TEXT(A1,"d/m/yy")&" ")-1),"/","</b><b>")&"</b></a>","//b["&{2,1,3}&"]"))+MID(TEXT(A1,"\ hh:mm AM/PM"),FIND(" ",TEXT(A1,"\ hh:mm AM/PM"))+1,99)
and then format the output as desired.
Note: I am US based so I had to reverse it in the demo.
Another Note: If one is US based dealing with UK dates then change the d/m/yy to m/d/yy and it will work.
Trying to convert Italian date. I got dates from pdf like 21 Giu 2020 which I paste into Excel and then try to format cells and choose option "Date" and 14-mar-12 there but it does not respond. It does only when I change 21 Giu 2020 to 21.12.2020 then it is 21-giu-2020.
Is there any way to automate it, else I need to search upon Italian months and conver Ago to 08, Giu to 12 etc etc?
If your locale isn't Italian, I doubt that Excel would ever recognize a string like that as a date. Here is a formula that would create the date for you:
=DATE(RIGHT(A1,4),MATCH(MID(A1,4,3),TEXT(DATE(2020,SEQUENCE(12),1),"[$-410]mmm"),0),LEFT(A1,2))
The avobe usage of SEQUENCE requires ExcelO365. I also made the assumption your text is always in the pattern "dd mmm yyyy".
You'll end up with a date which you can then use number formatting of your liking.
Have a large raw data dump and I am trying to format the dates into a consistent format.
As you can see from the screenshot, there are two main formats, one custom mm-dd-yyyy hh:mm AM/PM and mm/dd/yyyy hh:mm:ss. One is stored as general, while the other as a custom value.
I've tried to =left(A2,8), converting via text() and using text to columns, but can't bring the values to a consistent value.
It appears that your Windows regional settings for Short Date are DMY or similar. It is likely your data dump is in MDY format. That is why A2 and A4 are being converted to "real dates" (although incorrectly), and A3 is not since Excel does not know what do with month = 13. You will note that A2 is 1-Dec-2015 and I suspect that in the original data it is 12-Jan-2015.
EDIT: To expand a bit on the explanation. When something that looks like a date or time is entered into an Excel cell, Excel tries to change the result to a date, parsing the input according to the Windows Regional Short Date format. This sometimes has an undesireable outcome. For example, if your Windows Format is MDY but the date is input as DMY, input with days <=12 will be converted incorrectly, and input with days > 12 will be retained as text. This behavior cannot be "turned off" and causes many complaints from those who want to enter data that looks like a date, but is not. (For example, entering an odds ratio as 1-10 or 12:3 will get converted to a date or a time)
Several options
Change the output of the data so as to have the dates in DMY format.
Instead of OPENing the data file, do a Text Import. In later versions of Excel, you will find this on the Data Ribbon ► Get External Data ► From Text. This will open the Text import wizard and allow you to specify the MDY format of the incoming data before Excel transforms it.
After you have done one of the above, the result will be a "real" Excel date or date/time and you can format it how you like.
Write a IF() where if column B specifies "Custom" change format to standard.
For AM, just remove it(replace AM with nothing). For PM add 12 to the hours and replace PM with nothing.
For custom Replace - with / and append :00 to add the seconds to the custom ones too.
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))
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