How to properly format dates in Google Sheets or Microsoft Excel - excel

I have a spreadsheet I need to make in Google Sheets. The source of some of the data is exported to an Excel sheet. The data arrives in a dd/mm/yyyy format and I need to display it in a MON d format (Ex Sep 5).
The problem is both excel and sheets look at the date that arrives and think it is mm/dd/yyyy.
For example, 02/08/2022 is believed to be Febuary 8 even though it should be Aug 2. The problem then arises that neither of these platforms end up knowing how to convert this to Aug 2 and I end up having to do this manually.
Does anyone know how to get around this?
I have tried adjusting the format of the date, as well as using DateValue to convert (this fails since it understands the date as mm/dd/yyyy even when it is dd/mm/yyyy).
Any leads would be appreciated!
Thanks!

In Google Sheets, choose File > Settings > Locale and select a locale that uses the dd/mm/yyyy date format, before importing the data. You can then format the date column the way you prefer.

in gs:
=TEXT(REGEXREPLACE(A1&""; "(\d+)\/(\d+)\/(\d+)"; "$1/$1/$3"); "mmm d")

Try the following and format the result to your liking
=INDEX(IF(ISNUMBER(U2:U5),U2:U5,
IF(U2:U5=DATEVALUE("1899-12-30"),,
(MID(U2:U5,4,3)&LEFT(U2:U5,3)&RIGHT(U2:U5,4))*1)))
(Do adjust the formula according to your ranges and locale)
Functions used:
INDEX
IF
DATEVALUE
ISNUMBER
TRUNC
MID
LEFT
RIGHT

Well, for a formulaic solution, if the date is in A1, then the following places the correct date in B1:
=DATEVALUE(TEXT(A1,"DD/MM/YYYY"))
The TEXT function makes a string that will be the same form as your imported string out of the date produced during import. DATEVALUE then gives the proper date you desired.
The trick is in the TEXT step in which you reverse month and day in the string for DATEVALUE.
Naturally, instead of a helper column, it could just be wrapped around any reference to a date from column A, though one would have to remember to do so for all the years the spreadsheet is in use.
If you are importing, not just opening a .CSV file via File|Open and going from there, you have an opportunity to solve all your problems. You use the Ribbon menuing system's Data menu, select the very leftmost thing, Get Data and from the (no arguing THIS isn't a menu) menu that drops down, Legacy Wizards, then finally From Text (Legacy) which will open the old Excel Import Wizard. (You may notice this is very like the Data|Text to Columns Ribbon menu choice and that is because that choice is the old wizard minus the steps at the start that go looking to another file for the data because it knows, by law, that it has to already be in the spreadsheet... in other words, it looks the same because it IS the same.)
Then make selections for the first couple dialogs it presents you to get to the dialog in which you tell it to import columns as whatever: general (let Excel decide), text, date, and do not import. Choose Date and make the selection of DMY to import them properly as you desire them to be so you are never presented with the problem at all.
As you might guess, you can use the abbreviated wizard via the "Text to Columns" feature to do the same thing after import when you see they are reversed. Since it is a single column of data, the result will overwrite the original simplifying your work.
Why does this happen at all? Well, the "locale" folks have the idea. When Excel imports numbers that are in a form it recognizes could be a date, it looks to the operating system settings for the selected ways dates are understood. So if your operating system believes a date should be displayed "Month Day, Year" and Excel has a set of data it thinks fits that mold, it will convert them all using it. So you get those Feb 8's rather than Aug 2's.
Interestingly, it does two other things of note:
It looks at 8, count 'em, 8 rows of data to decide the data fits the pattern. Even with 1,000,000 rows to import, it looks at... 8.
Then it does them ALL as if God himself wrote the "8"... and dates like 25/03/2022 get imported as text not a real date, because they (oh, obviously) can't be dates... "25" can't be a month!
It IS possible to change settings (DEEP settings) to make Excel consider X number of rows in a data set before deciding such things. I found them here, on the internet, once upon a time, though I shouldn't like trying to find them again. It will consider up to a million rows in such an import, but... that'd make it pret-ty slow. And that's a million rows for EACH data column. I won't even say that "adds up" - I'll point out it "multiplies up."
Another technique is to add some number of starting rows to force the desired pattern onto the import. I've heard it works in TIME column imports so it ought to in DATE column imports but I've not verified such.
My bet is you will find the use of the "Text to Columns" feature of most use if you can use a hands-on approach - it does require literal action on your part, but is a fast operation. If you will see others using the spreadsheet though... well, you need a formulaic solution or a VBA one (macro with button for them to have some fun clicking as their reward for doing what they were trained to do instead of complaining to the boss you make bad spreadsheets). For a formulaic solution, the above formula is simple.
Last thought though: there's no error-checking and error-overcoming in it. So a date like "25/03/2022" in the data that imported as literal text is a problem. For handling the latter, an up-to-date approach could be:
=IF(TYPE(A1)=1,DATEVALUE(TEXT(A1,"dd/mm/yyyy")),DATE(INDEX(TEXTSPLIT(A1,"/"),1,3),INDEX(TEXTSPLIT(A1,"/"),1,2),INDEX(TEXTSPLIT(A1,"/"),1,1)))
in which the DATE(etc. portion handles finding text of the "25/03/2022" kind. Lots of less up-to-date ways to split the text Excel would have placed in the column, but since demonstrating what to do if it existed was the point, I took the easy way out. (Tried for a simple version but it wouldn't take INDEX(TEXTSPLIT(A1,"/"),1,{3,2,1}) from me for the input parameters to DATE.) TYPE will give a 1 if Excel imported a datum as a date (number), and a 2 if brought in as text. If empty or strange strings could exist, you'll need to deal with what those present you as well.

Related

Locale-independent Text function in Excel

I need to format dates in excel, and I'm trying to use the TEXT formula. The problem is that Excel's intepretation of the arguments changes when the locale changes.
For example: if I have a date in cell A1, that i'd like to convert to text, in the year-month-day-format, I have to use =TEXT(A1, "yyyy-mm-dd") if my PC has an English-language locale, but =TEXT(A1, "jjjj-MM-tt") (I kid you not, the M has to be upper case) if it has a German-language locale. This makes the document unportable. (The second argument is plain text and therefore not converted when changing locale.)
Remarks:
This is just an example, I know I could do the long =YEAR(A1) & "-" & TEXT(MONTH(A1), "00") & "-" & TEXT(DAY(A1), "00") in this case. I'm wondering about the more general case.
The date should not just be displayed in a certain format, it should actually be a string. For someone viewing the file this doesn't make a difference, but when using it in other formulas, it does.
I could write a UDF in VBA to solve the issue, but I cannot use VBA in this document.
I do not care about changing the names of the months etc. It's fine, if the name of the month is June or Juni depending on the locale.
I want to stress that the issue occurs due to the PC's locale - not due to the GUI language of the MS Office version. In the example above, Excel's GUI and formulas were in English in both examples; I just changed the locale on the machine.
Many thanks
Here is a slightly cheaty method: Use a VLOOKUP on a value that will change based on your System Language - for example TEXT(1,"MMMM")
=VLOOKUP(TEXT(1,"MMMM"),{"January","yyyy-MM-dd";"Januar","jjjj-MM-tt"},2,FALSE)
In English: Text(1,"MMMM") = "January", so we do a VLOOKUP on the Array below to get "yyyy-MM-dd"
"January" , "yyyy-MM-dd" ;
"Januar" , "jjjj-MM-tt"
Auf Deutsche, Text(1,"MMMM") = "Januar", also wir machen einen SVERWEIS auf dem Array oben, um "jjjj-MM-tt" zu erhalten! :)
Then, just use that in your TEXT function:
=TEXT(A1, VLOOKUP(TEXT(1,"MMMM"),{"January","yyyy-MM-dd";"Januar","jjjj-MM-tt"},2,FALSE))
Obviously, the main reason this works is that TEXT(1,"MMMM") is valid for both German and English. If you are using something like Filipino (where "Month" is "Buwan") then you might find some issues finding a mutually intelligible formatting input.
I found another possibility. It is not perfect in all cases (see below) but it also works with number formats to be locale independent. As I have the same issue with mixed language versions.
For this you make your own function in vba. Open the developer tools with Alt+F11 and create a new module file. Inside the module file paste something like this:
Function FormatString(inputData, formatingString As String) As String
FormatString = Format(inputData, formatingString)
End Function
Then you can use it in cell formulas with english formating strings. Like:
= FormatString(A1; "yyyy-mm-dd")
Advantage: It also works with number formats:
= FormatString(A1; "00.00")
In case (like Germany) your decimal separator is not a .
Drawbacks:
1 Not identical to TEXT function
this doesn't always work with date formatting as maybe expected and not exactly the same as the TEXT function:
FormatString(1; "MMMM")
does not return "January" but "December" because the 1 is taken as a date. Which is something like 31.12.1899.
2 Has to be saved with macros
You have to save the file as *.xlsm for this to work
Note (1): this answers only the case for locale-independent TEXT to format numbers with decimal symbols and digit grouping symbols. For date formatting, see Chronocidal's answer.
Note (2): this answer does not use VBA functions, which would require enabling macros. Enabling macros may not be possible depending on the company's security policy. If enabling macros is an option, Uwe Hafner's answer would be easier.
You can detect the decimal symbol and digit grouping symbol as follows. Enter the number 1 in a specific cell (e.g. A1) and the number 1000 in another cell (e.g. A2).
Decimal symbol: =IF(TEXT(INDIRECT("A1"),"0,00")="001",".",",")
Digit grouping symbol1: =IF(TEXT(INDIRECT("A2"),"#,###")="1000,",".",",")
This is assuming that the decimal symbol is either . or , and the digit grouping symbol is either , or . respectively. This will not detect unusual digit grouping symbols like (space) or ' (apostrophe).
With this information, you can set up a cell (or cells) with a formula that results in the format code you need to apply.
Suppose you need to format a number to two decimal digits and using the digit grouping symbol. You can assume that if the decimal symbol is . then the digit grouping symbol will be , and vice versa. You can do the following:
A1: 1
A2 (the formatting string): =IF(TEXT(INDIRECT("A1"),"0,00")="001","#,##0.00","#.##0,00")
A3 (contains an arbitrary number you wish to format)
A4 (the formatted number): =TEXT(A3,A2)
Technical note: the INDIRECT function is used intentionally because it is a volatile function. This guarantees that the formatting string and anything dependent on it is recalculated even if no data changed in the Excel document. If INDIRECT is not used, Excel caches results and will not recalculate the formatting string when the Excel document is opened on a PC with different locale settings.
1 - Also known as Thousands separator
The easy fix, whether directly custom formatting a cell or using TEXT(), is to use a country code for a language you know the proper formatting codes for.
For instance, I am in the US, have a US version of Excel, and am familiar with its date code formats. So I'd want to use them and to ensure they "come out" regardless of anyone's Windows or Excel version, or the country they are in, I'd do it like the following (for TEXT(), let's say, but it'd be the same idea in custom formatting):
=TEXT(A1,[$-en-US]"yyyy-mm-dd")
The function would collect the value in A1, ask Excel to treat it as a date, Excel would and would say fine, it's cool (i.e.: the value is, say, 43857 and not "horse") because it is a positive number which is a requirement for anything to be treated as a date, and let the function move on to rendering it as a date in the manner prescribed. Rather than giving an #ERROR! as it would for "horse" or -6.
The function would then read the formatting string and see the language code. It would then drop the usual set of formatting codes it loaded upon starting up and load in the formatting codes for English ("en") and in particular, US English ("US"). The rest of the string uses codes from that set so it would interpret them properly and send an appropriate string back to TEXT() for it to display in the cell (and pass on to other formulas if such exist).
I have no way to test the following, but I assume that if one were to use a format that displayed day of the week names or month names, they would be from the same language set. In other words, Excel would not think that even though you specified a country and language that you still wanted, say, Dutch or Congolese month names. So that kind of thing would still need addressed, but would be an easy fix too just involving, say, a simple lookup one could add though it'd be "fun" setting up the lookup table for each language one wanted to accomodate...
However, the basic issue that arises with this problem in general, is very, very easily solved with the country codes. They aren't even hard or arcane anymore now that the [$-409] syntax has been replaced with things like [$-en-us] and [$-he-IL] and so on.

excel comparison of two datasets

I am having a little difficulty conceptually understanding how to complete a task. Please forgive the context, but it will help.
I have a set of timetable information that contains the following
Date_Start (mm/dd/yyyy hh:mm:ss)
Date_End (mm/dd/yyyy hh:mm:ss)
Activity_location (String, code, example: B/B/012)
Other information that is not important
We have performed an audit (people going and doing a manual check on room occupancy). This audit was done using a google form which has now produced a spreadsheet. Unfortunately this doesn't quite match the format of the other one and instead contains:
Date
Time
B/B/012
B/B/011
... etc.
The problem is that each room is an individual column, regardless of if it was audited, which produces .... a lot of columns. I have already combined the Date and Time from the second dataset to produce a comparable datetime.
My task it to compare the information, so I have the timetable data (what should have happened) and I have the audit information (what did happen) and I need to find any discrepancies.
I am just having a little difficulty understanding how I might get these datasets into a format where I can compare them. I would really appreciate any help you might be able to give.
If Excel and you do have the dates (stripped from unneeded characters) in a column, you simply need to tell Excel how to interpret these values as dates. For instance, if you have the value 1/3/16, Excel may interpret it as March 1st, 2016 or Jan 3td, 2016.
To tell Excel how to interpret dates, you select the column (all cells in the column having values), right-click and select Format cells.... There, you can tell Excel that the value should be read as dd/mm/yy or mm/dd/yy.
Once you have Excel fully aware of the meaning of those dates, you can simply compare them (e.g. if(B3>G3... will check if the date in B3 is later than that in G3).
Hope this assists you to proceed.
UPDATE
Based on the exchange through comments, here is my final answer.
If you need to establish a relation (say between spreadsheet "A" and spreadsheet "B") when not only there is a on-to-one relation between columns/rows of both sheets, and (even worse) the one-to-many correlation is not predictable (meaning, in one case you have a one-to-one, in the next a one-to-4 and in the next a one-to-17), the only solution is either pivoting one of the tables or writing some MACROS.
I don't see any other way our of this. Sorry.

How to format different DOB formats in Excel?

The image below shows different formats for DOB, what is the easiest way to format them to dd/mm/yyyy? The dates on the right are correct however the dates on the left are back to front and missing a 0 for all single numbers.
I would be willing to bet that 08/03/1997 was not originally 08-Mar-1997 but started out as 03-Aug-1997. Same goes for all of the other ambiguous DMY/MDY dates that Excel wrongly converted during the text import. Some dates remained as text because (as in A3) there are not 13 months in a year.
It makes no sense to convert the rest of the data now that half of it is already wrong. Abandon the import and then import it properly.
I could regurgitate the narrative from Excel VBA - Convert Text to Date but it has already been adequately described there. In short, bring the text back in with Data ► Get External Data ► From Text and specify the correct date conversion mask in the Text Import wizard. In VBA, use the Workbooks.OpenText method and specify the xlColumnDataType as MDY.

MS Excel Time Format (Not Function)

I am having a problem with Excel's time format. I am trying to log amount of time spent on things. (and i want to be lazy) I try logging the time and excel converts it to a date. I know why it does this, but not how to stop it. I have even tried a custom format so that excel wont convert it, but excel somehow overrides my customization. Im trying to log four minutes and 24 seconds. i have tried doing 00424, 000424, 424, and a few others. I have tried 4:24 and excel changes that to 24:00. like i said, i have tried customization and i have tried pre-made formats, but nothing fixes it. Preferably i'd like to be lazy and just type in 424 and have it converted to 4:24. Any help?
Thanks, Greg
Use two columns, A where you enter values, B where you display them.
In B1 and below put:
=(INT(A1/100)*60+MOD(A1,100))/60/60/24
Then format column B as custom M:SS
Change the formatting of the cell to "Text" .. then type whatever you want: ie "4:24", "424", "0424", etc all work. No fancy formatting, but the data is in. Next, take the next cell and parse/format it (again in text) the way you want: So if you're input is simply "424" (in A1) as you want ... just do a: =concatenate(mid(a1,1,len(a1)-2),":",mid(a1,len(a1)-1,2))

Some but not all Excel numbers show as a date

I have a big .xls file. Some numbers show as a date.
31.08 shows as 31.aug
31.13 shows as 31.13 (that is what i want all columns to be)
When I reformat 31.aug to number it shows as 40768,00
I have found no ways to convert 31.aug to 31.08 as a number. All I am able to do is to reformat 31.aug as d.mm and then it shows as 31.08 and when I try to reformat it from 31.08 to number it shows as 40768,00. No way to cheat Excel using different types of cell formats.
How's your regional settings? There are some Regions where the short date is identified by dd.mm.yyyy. (Estonian, for instance). Maybe if you change the regional settings for US / UK and paste the data again it won't be changed.
Worked in a small test I did here. Hope it helps.
Internally Excel stores Dates as integer. 1 is January 1. 1900. If you entered something that Excel interprets as a date then it will be converted into an integer. I think from this point on there is no way back.
There is an setting in Options on the tab "international" where you can define your decimal separator. If you set this to ".", then your Excel should accept 30.12 as decimal number and not as date.
As pointed out by others, Excel interprets some of your data as a date instead of a number, which depends on your regional settings. To avoid this happening try Tiago's and stema's responses, they will work depending on your regional settings.
To repair your problem in a large file after it has happened without re-entering/re-importing your data, you can use something like
=DAY(B5)+MONTH(B5)/100
to convert a "date" back to a number. Excel will still display it as a date when you first enter this, but when you reformat it as "Number" now it will display the value you originally entered.
Since your column seems to contain a mix between correct numbers and dates, you need to add an if() construct to separate the two cases. If you haven't changed the display format yet (i.e. it still displays 31.Aug) you can use
=IF(LEFT(CELL("format";B7);1)="D";DAY(B7)+MONTH(B7)/100;B7)
which checks if the format is a "D"ate format. If you have already changed the format to Number, but know all your correct data is below 40000, you can use
=IF(B5>40000;DAY(B5)+MONTH(B5)/100;B5)
As suggested above, go to Control Panel - Region and Language - Advanced Settings - Numbers - and change the Decimal Symbol from "," to "."
Good luck!
The data you are pasting, is it by any chance a pivot table.
For example, like you, I am copying a lot of data into a large spreadsheet. The data I am copying is from another sheet and it is a pivot table.
If I paste normally, half will show up as numbers, which they are in the source file and half will show up as dates, for no reason, which drives me insane.
If I Paste->Values however, they will all show up as numbers, and as I don't need the pivot functionality in the destination file this solution is fine.
All you have to do is format cell.
1-right click on the cell where you want to insert the number.
2-then click on Number and select 'General' from the number menu.
Hope this will help future people with the same issue.

Resources