How to convert Excel date/time to epoch - excel

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.

Related

Unix Timestamp to CET excel formula

I am trying to convert a Unix Timestamp to CET
I used the formula I found in this site, asked by another user
=(A1/86400)+DATE(1970;1;1) - since I live in German',' were replaced in formula to ';'
but it does not work for me, the result I get is #############################
for example timestamp 1629294665262271969 should give Wed Aug 18 2021 14:51:05
but I get 'dates and time that are negative or too large are shown as ######################'
I thought that it might be because it could be in milliseconds so I divided by another 1000 but the result is the same
any advice will be welcomed,
Much appreciated,
It's not in miliseconds all in seconds. Excel dating system starts from 01.01.1900 but Epoch timing starts from 01.01.1970 so we need to convert two dates to same value that's the reason why we add or remove =DATE(1970;1;1)
When you look at a date in excel by using =VALUE(A1) formula you will see some number. These numbers shows you how many days past from 01.01.1900 to given date. So you multiply the value with 86400 to convert day values to seconds. That is the idea how it works.
I wrote the formula you need but you also need to clear current format of cells if you see something like ########### it's probably there was a date formatted value before you write the formula.
Formula examples below.
Date to Epoch time
A1 is 19.08.2021. Formula on B1 is =VALUE(A1-DATE(1970;1;1))*86400. Result: 1629331200
Epoch to Date time
A1 is 1629331200. Formula on B1 is =(A1/86400)+DATE(1970;1;1). Result: 19.08.2021

Excel - time of year/day/date/time to determine peak/offpeak

I'm looking for a formula in excel that lets me know if a date each date/time is peak or off-peak (electricity use data). Peak/off-peak is determined by the season, day of the week and time.
Peak equals:
JUNE-SEPTEMBER (SUMMER)
- mon, tue, wed thurs, fri and hour 9:00-18:00
OCTOBER-MAY (WINTER)
-mon, tue, wed, thurs, fri and hours 8:00-21:00
Off-peak - All other hours. (ie where this is false)
I have the code to determine if something is off/on peak individually:
June-September
IF(AND(WEEKDAY(A2,2)<=5,WEEKDAY(A2,2)>=1,A2-INT(A2)>=0.375,A2-INT(A2)<=0.75),"peak","off-peak"))
October-May
IF(AND(WEEKDAY(A2,2)<=5,WEEKDAY(A2,2)>=1,A2-INT(A2)>=0.333,A2-INT(A2)<=0.875),"peak","off-peak"))
However, my problem is I don't know how to put these together with a date like
01/02/2019 09:00 to do the first step to determine if it qualifies as SUMMER or WINTER. Would this be workable with a nested IF in excel? I can't seem to get this.
Making a separate column with an If statement that shows if the date is winter or summer is easy, and I've done it. But I still don't know how to combine this with the above functions.
Many thanks.
Excel - day/date/time to determine peak/offpeak
I want the output to just tell me peak or off peak, but the variable hours depending on the time of year is tripping me up.
Yeah nested if statements. If you've got your formula for SUMMER/WINTER in A1, then nest like this:
=IF($A$1="SUMMER",IF(AND(WEEKDAY(A2,2)<=5,WEEKDAY(A2,2)>=1,A2-INT(A2)>=0.375,A2-INT(A2)<=0.75),"peak","off-peak")),IF(AND(WEEKDAY(A2,2)<=5,WEEKDAY(A2,2)>=1,A2-INT(A2)>=0.333,A2-INT(A2)<=0.875),"peak","off-peak")))
The WEEKDAY function is really MOD(<date>, 7) with some adjustments. Using MOD, anything less than 2 is Saturday or Sunday.
The HOUR function quickly converts true time to an integer that can be compared to a set of paramters which are adjusted for summer/winter.
=IF(OR(MOD(A2, 7)<2, HOUR(A2)<(9-ISNA(MATCH(MONTH(A2), {6,7,8,9}, 0))), HOUR(A2)>(18+ISNA(MATCH(MONTH(A2), {6,7,8,9}, 0))*3)), "off-", "")&"peak"

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.

Excel parsing (xls) date error

I'm working on a project where I have to parse excel files for a client to extract data. An odd thing is popping up here: when I parse a date in the format of 5/9 (may 9th) in the excel sheet, I get 39577 in my program. I'm not sure if the year is encoded here (it is 2008 for these sheets).
Are these dates the number of days since some sort of epoch?
Does anyone know how to convert these numbers to something meaningful? I'm not looking for a solution that would convert these properly at time of parsing from the excel file (we already have thousands of extracted files that required a human to select relevant information - re-doing the extraction is not an option).
Excel stores dates as the number of days since 0-JAN-1900 (so 1-JAN-1900 would have a value of "1"). You can find a really good breakdown of how Excel handles dates and times here:
Dates And Times In Excel
When dates appear on screen in Excel as "5/9", "May 9th", or some such, it is a trick of the formatting and is not representative of the underlying data value. It sounds like your parsing program is pulling the underlying value, not the formatted date. In order to suggest a fix, though, I need to know what your parsing program is (Excel macro, formula, outside code, etc.).
DateTime.FromOADate (if you're using .NET) is the method you want. Excel dates are stored as doubles. If you have dates in the first two months of 1900 you might get bit by the Excel 1900 leap year bug.
From http://msdn.microsoft.com/en-us/library/system.datetime.fromoadate.aspx:
Double-precision floating-point number
that represents a date as the number
of days before or after the base date,
midnight, 30 December 1899. The sign
and integral part of d encode the date
as a positive or negative day
displacement from 30 December 1899,
and the absolute value of the
fractional part of d encodes the time
of day as a fraction of a day
displacement from midnight. d must be
a value between negative 657435.0
through positive 2958466.0.
All you need to do is format the cells correctly. Or am I misunderstanding your question -- are you saying you want to do it OUTSIDE of Excel? I wasn't sure. I'll delete this answer if it turns out to be stupid.

Basic excel date and time problem

Have a list of dates in excel in the format (this comes originally from csv):
23/11/09 07:27:02
23/11/09 08:01:50
23/11/09 08:38:58
23/11/09 09:40:01
What I want to do is count the number of these falling between hour blocks, like 7-8, 8-9, 9-10 etc
Not sure how to get started, but one idea was just to put logic statements comparing the dates between these blocks, then adding the total "trues"
I can't get it to compare properly. When I type it the hour block marks,
e.g. 23/11/09 08:00
excel actually shows that as
23/11/2009 8:00:00 AM
and the compare doesn't work. Well actually it does the opposite of what it should.
that is:
=IF(C5>L1,IF(C5<M1,TRUE,FALSE),FALSE)
C5 being date in top codeblock, L1 and M1 being the hour blocks I manually entered in the second code block.
Has anyone got any ideas?
=hour(a1)=7
will return true if the time of the date/time value in cell A1 is between 7 and 8 (AM) and will otherwise return false.
Excel stores dates as number of days since 1900 or 1904 depending on your setting and the time as a fraction of the days. So 11:59 am 4th of July 1960 is held internally as '22101.4993055556'.
As such you cannot do plain charactrer string comparisons on dates. However ther ar lots of nifty time/date functions available to you.
You probably want :
=IF(HOUR(B1) > 8,IF(HOUR(B1)<12,"YES","NO"),"NO")
You should use Excel functions, like HOUR(), to extract the parts of the times, and apply the logic tests to those extracted values.

Resources