Excel 2010 - change US dates to UK format - excel

I have imported a CSV file with 2 long columns of dates. These dates are in the US format Aug/28/2013 and I want them to be in the standard UK dd/mm/yyyy format.
I have tried formatting the cells as US dates and then converting them to number-only formats, and various other permutations within the Date format box, but with no success.
Can anyone rid me of these awful US dates please?

Another solution without using a formula:
Select the relevant columns
Choose Data → Text to Columns…
Select “Delimited” and click Next
Untick all delimiters and click Next
Select data column format “Date: MDY” and click Finish
The dates should now be converted to UK dates.

The problem is that a US date can parsed by Excel as a UK date when the day is less than 13. When this happens Excel converts it to the localized UK serial (date) number.
So 03/19/2014 is obviously a US date of the 19th of March. However 05/03/2014 is ambiguous so Excel parses it the local date format as the 5th of March, rather than the US 3rd of May. Any formula has to check if Excel has stored the US Date as a UK date. A UK date will be stored in Excel as a number.
=IF(ISNUMBER(A2),DATE(TEXT(A2,"yyyy"),TEXT(A2,"dd"),TEXT(A2,"mm")),DATE(RIGHT(A2,4),LEFT(A2,FIND("/",A2)-1),MID(A2,FIND("/",A2)+1,2)))
(For a US date in cell A2 and PC date is dd/mm/yy).
If ISNUMBER is true, the US date looks like a UK date and Excel has serialized it as a number. So can format the date as text and back to a date again. Note day is passed to the month parameter of the first DATE function to perform the conversion.
If ISNUMBER is false, its stored as a string as Excel doesn't convert a date string with >12 months. So use string functions to split it up for the DATE function.

I'm assuming that the date you received is formatted as text and that simply formatting it as date is not changing anything. You can run the following formula on the date:
=(MID(A1,FIND("/",A1)+1,FIND("/",A1,FIND("/",A1)+1)-FIND("/",A1)-1)&"-"&LEFT(A1,FIND("/",A1)-1)&"-"&RIGHT(A1,4))*1
If you get numbers, you just need to format it as dd/mm/yyyy and it should be good.

I tried some of the other suggestions but none seemed to work for me. In my case I was importing US dates in the form M/d/yyyy hh:mm:ss. If you don't mind using some VBA in your spreadsheet then the following function did the job for me:
Public Function USDate(ds As Variant) As Variant
Dim sp() As String
Dim spt() As String
Dim spt2() As String
If ds = vbNullString Then
USDate = ""
ElseIf IsNumeric(ds) Then
' Convert numeric US dates wrongly interpreted as UK i.e. 1/7/2017 as 7th January 2017
USDate = DateSerial(Year(ds), Day(ds), Month(ds))
Else
sp = Split(ds, "/") ' split the date portion
spt = Split(sp(2), " ") ' split the time from the year
spt2 = Split(spt(1), ":") 'split the time hms
USDate = DateSerial(spt(0), sp(0), sp(1)) + TimeSerial(spt2(0), spt2(1), spt2(2))
End If
End Function
Thanks for https://stackoverflow.com/users/845584/peterx pointing out - you will need to create the function in a VBA code module to use this technique.
Simply use it in a spreadsheet formulae for example =USDate(A2)

Related to this, the below simply formula can be helpful for changing a date from
"MM/DD/YYYY"
into
"DD/MM/YYYY".
=VALUE(TEXT(B2,"mm/dd/yyyy"))

We can get best of both world with this more concise formula:
=IF(ISNUMBER(A2),VALUE(TEXT(A2,"mm/dd/yyyy")),DATE(RIGHT(A2,4),LEFT(A2,FIND("/",A2)-1),MID(A2,FIND("/",A2)+1,2)))
Can't find anything shorter.

There was one more issue for me, as somehow the raw data was supposed to be read as a number, but it did not. Hence, i updated the formula with 1 final case:
=IFERROR(IF(ISNUMBER(A2),VALUE(TEXT(A2,"mm/dd/yyyy")),DATE(RIGHT(A2,4),LEFT(A2,FIND("/",A2)-1),MID(A2,FIND("/",A2)+1,2))),DATE(RIGHT(A2,4),LEFT(A2,FIND("/",A2)-1),MID(A2,FIND("/",A2)+1,1)))

This can be tricky when the dates in mixed format eg. UK and US in the same column. I have found an effective if inelegant solution:
Step1) Select the column containing the dates to be converted;
Step2) Format, Cells, Text;
Step3) Format, Cells, Date, US;
Step4) Data, Text to column, Next, Delimited, Next, delete all delimiters, Next, select format MDY;
Step5) Format, Cells, Date, UK.
Step4 had been suggested elsewhere, but that on it's own didn't do it for me. I am hoping to combine these steps into a macro but no success this far.

I couldn't get the most common answer to work, the process that worked for me was:
For date 10/04/2018 11:49:20, right-click cell and "Format Cells", "Number" tab and select "Custom" Category and then select mm/dd/yyyy hh:mm.

Assuming that you start with a string (and not an internal excel date number that is just formatted as US format - which is an easy fix), can someone tell me why this method doesn't work?
Use the DATEVALUE / TIMEVALUE functions to convert it into an excel internal formatted date number (You might need to MID() the string in case there are extra bits before or after).
Just make sure that your regional settings match the input date format (otherwise DATEVALUE will fail and you will get a #VALUE error).
Then set the cell format to display the way you want it (Custom format e.g. "dd/mm/yyyy hh:mm:ss").
If you also want to change the timezone, you can add on (hours/24) to the internal Excel excel formatted date number.

The above look impressively complex! Why any country should settle on a non-sequential date format escapes me! Say you have a US-format date (mm/dd/yy) in cell A1. To convert this to dd/mm/yy format as in the UK, just do:
=CONCATENATE(MID(A1,4,2),"/",MID(A1,1,2),"/",MID(A1,7,2))
This certainly works in LibreOffice and I hope also in Excel.

Related

How to extract data from a field that is apparently of the date / time format but is not really such a format

I have copied (copy/paste) a part of a home-page into an empty excel. One of the fields looks like this: 3140:01:00. If I check the format, it shows that the category is custom, and that the type is [t]:mm:ss. The problem is that I am only interested in the first 4 digits shown plus digits 6 and 7. If I change the format to e.g. text, I end up with a number. Probably a number, that identifies a specific date. In fact the first 4 digits are the length of a horse race! :-) I'm new at VB, but I have managed to clean up the rest of the information - but not this. Probably a known problem. Please help!
You will need to understand the difference between the value and the representation of your data (note that this is not VBA but Excel related). When you enter 3140:01:00 in a cell in Excel, Excel tries to understand what you enter. With the colon, it looks somehow like a time value, so Excel guesses that this a a time, convert what you enter into a date value (a date in Excel has automatically a time part) and put a number format that displays this date+time as [h]:mm:ss.
As I said, internally, what you entered is converted into a Date. Now a Date in Excel in internally stored as a number. If you set the number format to "Number", the cell will display 130.83402. This is because 3140 hours = 130 days + 20 hours. The 20 hours (plus the 1 minute) are stored as a fraction of a day (0.83402).
If you format the same value as Date/Time, you will see (depending on your regional settings) something like 05/09/1900 20:01:00 - because that is the 130th day in the Excel calendar (day 1 in Excel is 1/1/1900). Note that the value of the cell doesn't change, only the way it is displayed.
If you could prevent Excel to convert your input into a date, the solution would be to do string-handling, eg use the Split-function. When you format a cell as Text and enter 3140:01:00 manually, Excel leaves the string untouched and this would work. However, it seems that when you Paste the value into the cell, the number format is set automatically and the value is converted into a date even if the cell was formatted as Text before. I don't know if there is a way to tell Excel to not convert the data if it is pasted.
So what we can do instead is to convert the date value back into "hours", "minutes" and "seconds" - even if the "hours" are in fact something else (meters? yards? horse length?), and the minutes are probably also not minutes but whatever.
Several ways to do so.
If you don't mind that the strange pseudo-date value remains in your Excel (you can hide the column with that value), use just 2 simple formulas. Assuming your "date" is in D2:
use the formula =TRUNC(24*D2) to get the horse race length (the first number). We cannot use the Hour-formula here as this would return only 20 and not 3140.
use the formula =MINUTE(D2) to get the second number
use the formula =SECOND(D2) to get the third number
If you want to involve VBA:
Sub SplitStrangeDate(cell As Range)
If Not IsDate(cell) Then Exit Sub
Dim d As Date
d = cell.Value
Dim v1 As Long, v2 As Long, v3 As Long
v1 = CLng(d * 24)
v2 = Minute(d)
v3 = Second(d)
Debug.Print v1, v2, v3
End Sub

Formatting Dates (International and American in Same Column)

I am struggling with formatting dates in Excel and am looking for some help.
I have two different date formats in the same column:
MM/DD/YYYY
DD/MM/YY
I am struggling to change the DD/MM/YY to MM/DD/YYYY. I have tried so many things and can't get it to work.
Public Function dateguesser(inDate As String) As Date
dateguesser = CDate(Split(Split(inDate, " ")(0), ",")(0))
End Function
Things I have tried:
This solution ends up thinking that the DD in the DD/MM/YY format is the year because the year is not stored as four digits.
Tried to change MM/DD/YYYY to DD/MM/YY and then convert all of them to MM/DD/YYYY.
Text to columns
If you put this formula starting in B3, I think it should work for all values?
=IF(ISNUMBER(A3),A3,DATE(2000+RIGHT(A3,2),MID(A3,4,2)+0,LEFT(A3,2)+0))
You could also probably modify your custom function to something like this:
Public Function dateguesser(inDate As Variant) As Date
If IsNumeric(inDate) Then
dateguesser = inDate
Else
dateguesser = CDate(Split(Split(inDate, " ")(0), ",")(0))
End If
End Function
I'd add another column to convert to epoch and back to new date format. https://exceljet.net/formula/convert-unix-time-stamp-to-excel-date could help
Assuming the input is in A10:A11, I made for B10:
=((A10-DATE(1970,1,1))*86400/86400)+DATE(1970,1,1)
and applied new date format to B:B.

Excel TODAY() function in IF formula doesn't work properly

I have "Microsoft MonthView Control 6.0 (SP4)" component that enters the date to cell "D17" I want my function to compare today date to cells "D17" date and give certain message. So if date in cell "D17" is older than current date it will display "OFFER EXPIRED" message. In cell "D17" I currently have 1.10.2018. I use formula =IF(D17>TODAY();"OFFER EXPIRED";"") but it doesn't work. I have tried different cell Formatting options with no success. Any ideas where the problem can hide?
Please try:
=IF(1*SUBSTITUTE(D17;".";"/")>TODAY();"OFFER EXPIRED";"")
(Seeks to coerce Text 1.10.2018 into Number before making the comparison.)
Assuming that the date you have is in string format & in dd.mm.yyyy format. Use below code for the comparison:
=IF(DATE(
RIGHT(D17,4),
MID(D17,FIND(".",D17,1)+1, (
FIND(".",D17,FIND(".",D17,1)+1)-
FIND(".",D17,1))-1),
LEFT(D17,FIND(".",D17,1))
)<TODAY(),
"OFFER EXPIRED","")

day and month are reversed

I have a cell with the following content:
01/02/2015
The cell is date formatted.
Then I copy the value and put it in my module class:
Set Line = New CTravelLine
Line.Date= Cells(1, 8).value
Everything works fine until the moment I put this value in another cell:
The value 01/02/2015 becomes 02/01/2015.
I am using this format (dd/mm/yyyy). I have the impression that when the days are numerically lower than the month, the 2 values are reversed. The values are reversed whatever the method I tried:
Method 1:
Dim WrdArray() As String, datetest As String
WrdArray() = Split(travelLine.Date, "/")
datetest= WrdArray(0) & "/" & WrdArray(1) & "/" & WrdArray(2)
Cells(5, 5) = datetest
Method 2:
Cells(5, 5) = travelLine.Date
I don't understand how I can solve this problem.
This might have happened due to 'Regional formatting problem'.
Excel has a habit of forcing the American date format (mm/dd/yyyy) when the dates have been imported from another data source. So, if the day in your date happens to be 1 - 12, then Excel will switch the date to mm/dd/yyyy.
When dates are imported from a text file, there is an option in the VBA code to apply regional format which corrects this problem.
OR
Change number format of date column in excelsheet from 'date' format category to 'text'; save it.
(After Saving run the VBA Code if you have any. Now check whether the date format is 'text' or changed back to 'date'.)
If it has changed back to 'date' try to fix it as 'text'
If it's 'text'; Correct the erroneous date cells and save the excel sheet. This will make dates not to change automatically to American Format.
Long story short, I had a similar problem where the dates are working just fine in some cells but keep flipping in others regardless if I copy paste or enter manually, I did the whole data text to column and cell formatting solutions and all of that didn't work.
The solution actually is not in excel, it's in the region and language setting.
To have the dates display as MM/DD/YYYY in the formats tab change the format to US.
To have the dates display as DD/MM/YYYY in the formats tab change the format to UK.
I had the same issue as you .
Let me explain what I want to do :
I have a csv file with some date.
I copy a range of my sheet in variable table. The range contain some columns with dates.
I make some manipulations on my table (very basic ones)
I transpose my variable table (since only the last dimension of a variable table can be increase)
I put my variable table on a new sheet.
What I found:
There is no date issue after executing step 1-4. The date issue shows up when writing on the sheet...
Considering what Avidan said on the post of Feb 24 '15 at 13:36, I guess it is excel which forces the American format mm/dd/yyyy... So I just change the date format at the very beginning of my program :
Before starting any manipulation date:
do
.Cells("where my date is") = Format(.Cells("where my date is"), "mm dd yy")
execute your program
write the table on a sheet
back up to the date format you like directly on the sheet
Just use:
Line.Date = cDate(Cells(1, 8).value2)

EXCEL VBA CSV Date formatting issue

I am an Excel VBA newbie. My apologies if I am asking what seems to be an obvious question.
I have a CSV text file I am opening in an Excel worksheet. One row of the file contains date information formatted as "YY-MMM', (ie: 9-FEB, or 10-MAY). When the Excel spreadsheet opens the file the date information is changed to "mm/dd/yyyy" format and reads as 02/09/2009 or 05/10/2009 where the YY-MMM is now MM/YY/2009, (ie: the 9-FEB becomes 02/09/2009, and the 10-MAY becomes 05/10/2009).
I would like to know if it is possible to reformat the field from YY-MMM to mm/01/yyyy.
I have tried to parse the date field after converting it to text with
Range("B11", "IV11").NumberFormat = "text"
However, then the value is a serial date and non-parsable.
I have been unsuccessfully looking for a list of the NumberFormat options.
If you can point me in a direction it will be much appreciated.
Just to answer a part of your question, here is the list of date formatting options (excluding time):
d = day of month, e.g. 7
dd = zero-padded day of month, e.g. 07
ddd = abbreviated day name, e.g. Thu
dddd = full day name, e.g. Thursday
pretty much the same for month...
m = month number, e.g. 7
mm = zero padded month number, e.g. 07
mmm = abbreviated month name, e.g. Jul
mmmm = full month name, e.g. July
years are simpler...
yy = 2 digit year number, e.g. 09
yyyy = 4 digit year number, e.g. 2009
you can combine them and put whatever separators you like in them
e.g.
YY-MMM, 09-FEB
DDDD-DD-MMM-YY, Wednesday-04-Feb-09
dd/mm/yyyy, 04/02/2009
mm/dd/yyyy, 02/04/2009
I've just been trying a few things out and I think your best bet is to change the format in the text file to conform to a more standard date arrangement. If you can reverse them (e.g. MMM-YY) you'll be fine, or split them into separate columns (what if when you import you define - as a separator as well as comma?). This is one case where Excel trying to be clever is a pain.
HTH
If you are currently using File > Open to open the CSV file then try Data > Import External Data > Import Data instead. This brings up the text import wizard which might give you more flexibility in how to import the file. Specifically, it lets you declare a column in the file as being text so that Excel does not try to parse the value
As Simon has explained, your terminology for the current date format is not correct. 9-FEB corresponds to d-mmm format and 02-19-2009 (NB deliberately changed to an unambiguous date for this example; 02-09-2009 is 9th Feb in the US but 2nd Sept in the UK) corresponds to mm-dd-yyyy
If you wanted to change the NumberFormat to text for the range starting in cell B11 and ending in cell IV11 then you would use:
Range("B11:IV11").NumberFormat = "#"
# signifies text format and you need the : operator to indicate a contiguous range of cells. Your original example of Range("B11","IV11") actually indicates the union of cells B11 and IV11 and thus would only have affected two cells
It is unusual to have your data structured in rows rather than columns. Rather than having your dates in row 11, it would be more common to have them in column K instead so that you would use Range("K2:K65535") or just Rows("K") Most of the built-in Excel stuff like Sort and AutoFilter assume that your data is laid out in columns anyway. Row 1 traditionally contains the column names and then each subsequent row contains an individual record. You might want to look at how your CSV file is being generated to see if you can switich it to a more usable column-based structure
If you have a date like 19-FEB as text in a cell then, assuming that you always want the year part to be the current year, you can change it to the first day of the month in mm/dd/yyyy format in VBA. This example only changes one cell but you can use it as the basis for a wider solution:
Option Explicit
Sub main()
Dim strOrigValue As String
With ThisWorkbook.Worksheets("Sheet1").Range("B11")
strOrigValue = .Value
.NumberFormat = "mm/dd/yyyy"
.Value = DateSerial(Year(Date), Month(strOrigValue), 1)
End With
End Sub
DateSerial creates a date serial number from the given year, month and day. Year(Date) extracts the year part from the current system date, Month(strOrigValue) takes the month part from the data that's already in the cell and we want the first day of the month
In order to read in a date, you can do something like this:
myDate = DateValue(dateText)
although there are caveats to how excel may interpret dates, and what technical limits it has in what dates it may store. (you may need to read up on maximum and minimum dates, and how excel interprets two-digit years >30 vs <30, among other things)
In order to format the date, you can use one of three functions:
myDateText = DatePart("yyyy", myDate) & "/" & DatePart("mm", myDate) & "/" & DatePart("dd", myDate)
or this
Format (myDate, "yyyy/mm/dd")
or this
FormatDateTime(#1/1/2020#, vbShortDate)
You can search google for more documentation on each function. For the format function, you will want to find a good reference -- I've seen some rather incomplete ones. MSDN tends to have good ones, but not always. Feel free to bump this answer if you want more details.
This assumes your 2 digit year is 2000. Once you end up with your incorrect date 9-Feb = 02/02/2009, you can convert this to the date you want with the formula: =DATE(Day(B11)+2000,Month(B11),1)

Resources