Is there a bug in Excel concerning dates? - excel

Please don't ask how I stumbled upon this but please try the following:
Type "60" into a cell in Excel.
Set the data type to a date format
The date displayed (in my case at least) is February 29th, 1900.
However, February 29th, 1900 never occurred since 1900 was not a leap year (see Wikipedia).
Is this a bug or am I missing something?

No, it is not a bug in Excel and yes you are missing something.
Excel implemented the dates to match with Lotus 1-2-3 for which compatibility was required at the time so was designed to have this error. Lotus 1-2-3 had the bug of thinking 1900 was a leap year.
See Microsoft's documentation for the reasons why not to fix it.

Please. It is a bug. It was never documented in the original Excel for Windows specifications. The original Excel which was on the Mac years before it was on any Windows platform used a 1904-based date system. The original Excel authors did recognize the issue.
"Strictly speaking, that wasn't an issue with Excel 1.0, which ran only on Macs. The original version of the Mac (and Excel) supported dates starting in 1904, partly to avoid the issue with 1900 not being a leap year. It wasn't until Excel was ported to Windows (Excel 2.0) that the 1900 date system was introduced, so you can't blame any of us on the Excel 1.0 team for the decision (and so, of course, we wouldn't comment on it in the interview)."
https://www.geekwire.com/2015/recalc-or-die-30-years-later-microsoft-excel-1-0-vets-recount-a-project-that-defied-the-odds/
The bug was also on MS-BASIC and Multiplan.

It's not a bug - Excel accepts Feb 29th 1900 as a valid date. It's a historical issue.
Your 60 is being regarded as 'number of days since Jan 1 1900'.
More info:
http://polymathprogrammer.com/2009/10/26/the-leap-year-1900-bug-in-excel/

Related

Why doesn't Excel display argument hints for DATEDIF()?

Whenever I type a function into Microsoft Excel 2013, a small hint box is displayed which lists the function's arguments.
For some reason, this hint box doesn't appear when using DATEDIF(). I can't find it listed anywhere on the formula tab, nor am I shown any reminder of what arguments to use. Excel won't even autocomplete the formula name while typing.
Returns number of days between Nov 15-Nov 20
The formula works as expected once entered, but it's difficult to use properly when I need to have the arguments memorized.
Why isn't Excel displaying argument hints for DATEDIF()?
DATEDIF is an undocumented function in Excel
From Office Support:
Excel provides the DATEDIF function in order to support older
workbooks from Lotus 1-2-3. The DATEDIF function may calculate
incorrect results under certain scenarios.
From My Online Training Hub
For some reason Microsoft [doesn't] think the DATEDIF function is worthy
of any documentation since Excel 2000. In fact they say they only
include it in recent versions for backward compatibility.
From Happy LTD
Microsoft no longer supports this function in Excel. You can still use
it, but you don’t get any help with it.
A suggestion is: UDF replacement for Excel's DATEDIF function
Also visit:
Do not use the undocumented DATEDIF function
Introduction To The DATEDIF Function
The "DATEDIF" function has its origins in "LOTUS 1-2-3", and apparently Excel provides it for compatibility purposes. It has been available since Excel 5, but Excel 2000 is the only version that ever documented it in its Help system.
The function takes 3 arguments: start_date, end_date, and a code that represents the time unit of interest. I am including cells A1 & A2 for the purposes of the example. Substitute the cells you need to use.
The following code is an example:
=DATEDIF(A1, A2, "y") You must enclose the code in quotation marks.
The following table lists the codes available with the returns.
"y" The number of complete years in the period.
"m" The number of complete months in the period.
"d" The number of days in the period.
"md" The difference in days between the start_date and end_date. the
months
and years are ignored.
"ym" The difference in months between the start_date and end_date.
the days
and years are ignored.
"yd" The difference in days between the start_date and end_date. The
years
of the date are ignored.
NOTE, the start_date must be earlier than the end_date or the function will return an error.

How to take today's date and output it as mmddyy?

As the title asks, I want to format today's date into mmddyy ina variable for a macro to make saving a file easier. So since today's date is August 14, 2015 I want the variable to read 081415.
VBA equivalent is format. See here.
=Format(Date,"mmddyy")
You should also consider being vary careful when handling dates depending on your location.
A constant problem I had in the UK was excel trying to as usual help and insisting on interpreting some dates as US.
For example
1st August 2015 you would expect 010815 in the UK.
However excel would regularly give 080115 formatting as though it was US.
To be completely sure of the correct date, you should be explicit with the date eg.
format(dateserial(year(now()),month(now()),day(now())),"mmddyy")

How to convert Excel date/time to epoch

I Googled and found a formula that is supposed to convert from Excel date/time to epoch time. However, it's off (or I'm off) and I can't figure out what I'm doing wrong. I'm using http://www.epochconverter.com/ as my source of truth.
So the link I found says the formula is:
=(A1-25569)*86400
I think 25569 is the Excel value for DATEVALUE("1-1-1970") but for some reason my version of Excel says that's 24107 so I made some modifications and typed:
=((A2-DATEVALUE("1/1/1970"))*86400 - 8*3600 (for PST)
Now the number is a little more correct. However, it seems like I need to ADD 8*3600 instead of SUBTRACT.
Can someone explain to me why?
I thought PST is -8 from GMT.
For the sake of an answer:
Your Excel is using the 1904 date system.
IF your Excel times start off as ‘local’ don’t adjust for time zone. IF they start as GMT (a concept Excel does not have) then, because when a GMT clock ticks on to 16:00 a ‘local’ clock should tick on to 08:00, from Excel to ‘local’ means deducting 8 hours. Note that midnight 31/12/1969 is not the same instant GMT as PST.

Bug in VBA DateValue() in VBA Excel?

When I use the formula datevalue("01/01/1900") I get 1 and formatted as a date it shows as 01/01/1900
When I use VBA
.Range("A1").Value = DateValue("01/01/1900")
it shows up as "02/01/1900" in the cell
How is this possible?
If i use for example
.Range("A1").Value = DateValue("01/01/1901")
it works fine!
Head Melted!!!
Microsoft state that - "Using the default date system in Microsoft Excel for Windows, the date_text argument must represent a date between January 1, 1900 and December 31, 9999"
In short, Excel's DateTime epoch is not the same as VBA's DateTime epoch. Although, they are the same once you get past February 28th, 1900.
From Joel Spolksy's blog:
In most modern programming environments, dates are stored as real
numbers. The integer part of the number is the number of days since
some agreed-upon date in the past, called the epoch. In Excel, today's
date, June 16, 2006, is stored as 38884, counting days where January
1st, 1900 is 1.
I started working through the various date and time functions in Basic
and the date and time functions in Excel, trying things out, when I
noticed something strange in the Visual Basic documentation: Basic
uses December 31, 1899 as the epoch instead of January 1, 1900, but
for some reason, today's date was the same in Excel as it was in
Basic.
Huh?
I went to find an Excel developer who was old enough to remember why.
Ed Fries seemed to know the answer.
"Oh," he told me. "Check out February 28th, 1900."
"It's 59," I said.
"Now try March 1st."
"It's 61!"
"What happened to 60?" Ed asked.
"February 29th. 1900 was a leap year! It's divisible by 4!"
"Good guess, but no cigar," Ed said, and left me wondering for a
while.
Oops. I did some research. Years that are divisible by 100 are not
leap years, unless they're also divisible by 400.
1900 wasn't a leap year.
"It's a bug in Excel!" I exclaimed.
"Well, not really," said Ed. "We had to do it that way because we need
to be able to import Lotus 123 worksheets."
"So, it's a bug in Lotus 123?"
"Yeah, but probably an intentional one. Lotus had to fit in 640K.
That's not a lot of memory. If you ignore 1900, you can figure out if
a given year is a leap year just by looking to see if the rightmost
two bits are zero. That's really fast and easy. The Lotus guys
probably figured it didn't matter to be wrong for those two months way
in the past. It looks like the Basic guys wanted to be anal about
those two months, so they moved the epoch one day back."
"Aargh!" I said, and went off to study why there was a checkbox in the
options dialog called 1904 Date System.
The information below was taken from this Super User answer.
As described in Microsoft KB 214058:
Days of the week before March 1, 1900 are incorrect in Excel
MORE INFORMATION
When the date system in Microsoft Excel was originally created, it was designed to be fully compatible with date systems used by other spreadsheet programs.
However, in this date system, the year 1900 is incorrectly interpreted as a leap year. Because there is no February 29 ("leap day") in the year 1900, the day of the week for any date before March 1, 1900 (the day after the "leap day"), is not computed correctly.
The "other spreadsheet programs" refer to Lotus 1-2-3, which was quite popular back then, and incorrectly assumed that year 1900 was a leap year. This is explained in even more detail in KB 214326:
Excel 2000 incorrectly assumes that the year 1900 is a leap year
MORE INFORMATION
When Lotus 1-2-3 was first released, the program assumed that the year 1900 was a leap year, even though it actually was not a leap year. This made it easier for the program to handle leap years and caused no harm to almost all date calculations in Lotus 1-2-3.
When Microsoft Multiplan and Microsoft Excel were released, they also assumed that 1900 was a leap year. This assumption allowed Microsoft Multiplan and Microsoft Excel to use the same serial date system used by Lotus 1-2-3 and provide greater compatibility with Lotus 1-2-3. Treating 1900 as a leap year also made it easier for users to move worksheets from one program to the other.
Although it is technically possible to correct this behavior so that current versions of Microsoft Excel do not assume that 1900 is a leap year, the disadvantages of doing so outweigh the advantages.
If this behavior were to be corrected, many problems would arise, including the following:
Almost all dates in current Microsoft Excel worksheets and other documents would be decreased by one day. Correcting this shift would take considerable time and effort, especially in formulas that use dates.
Some functions, such as the WEEKDAY function, would return different values; this might cause formulas in worksheets to work incorrectly.
Correcting this behavior would break serial date compatibility between Microsoft Excel and other programs that use dates.
If the behavior remains uncorrected, only one problem occurs:
The WEEKDAY function returns incorrect values for dates before March 1, 1900. Because most users do not use dates before March 1, 1900, this problem is rare.

Why is this format "[$-F400]h:mm:ss\ AM/PM" a 24 hour format in Excel?

And yet this one [$-409]h:mm:ss\ AM/PM;# is not and shows AM/PM correctly?
I'm using NPOI to read from a Excel file and extract some useful data for my app. While I was parsing dates, I stumbled across this interesting case.
This date 16:00:00 (4 o'clock in the afternoon) with this format [$-F400]h:mm:ss\ AM/PM is showing as 16:00:00 in excel.
Yet a similar format, [$-409]h:mm:ss\ AM/PM;#, shows 04:00:00 PM, which I believe is correct because of the AM/PM notation at the end of the format string.
My excel version is this one: Microsoft® Office Excel® 2007 (12.0.6665.5003) SP3 MSO (12.0.6662.5000)
And I'm using the latest version of NPOI (got from the source code), which interprets [$-F400]h:mm:ss\ AM/PM as hh:mm:ss tt (which doesn't match the way excel shows it)
I know that [$-XXXX] is the locale, I don't know about ;#.
Why, depending on the circunstances, excel is completely ignoring the AM/PM part?
EDIT: It appears the # is a text-placeholder for AM/PM it seems. Is the absence of this token forcing excel to show a 24-hour format?
As #pnuts commented, this link indicates that Excel uses a special sequence for system default.
"The cause is, that Excel uses an (undocumented) escape sequence for
"system default". This is [$-F800] for date, long system default
[$-F400] for time, system default".
NPOI completely ignores locale information, as it's passed as a parameter to the DataFormatter. So, instead of using the system default, it parses the "AM/PM" part and shows a 12-hour format, instead of the 24-hour system default
I've solved my problem by changing the source code of the library to suit my needs in this case.

Resources