Excel - Convert JavaScript/Unix timestamp to date - excel

I'm wondering how to convert a timestamp to a date in Excel.
JavaScript timestamp:
1486812409161
Unix timestamp
1486812409
What Excel function / formula can convert to something like:
2/11/2017 11:26 AM (or any human readable date)
I did see this answer, but I can't get this to work for me (on Mac OS X / Excel 2011).
When I create a new cell and set it's value to the following formula:
= (MsValueCellReference / 86400000) + DATE(1970,1,1)
The result is: 41315.47696

I am using the current formula =(<javascriptTimestamp>)/(1000*60*60*24)+25569 and then formatting the cell with dd/mm/yyyy hh:mm:ss.
To explain the terms in the formula, the Javascript timestamp has milliseconds which accounts for the 1000. There are 60*60*24 seconds in one day.
Finally, Excel dates start on Jan 1 1900, and Javascript starts on Jan 1 1970. There are 25569 days between Jan 1 1900 and Jan 1 1970.

Excel is pretty happy to interconvert between dates and numbers, as you noticed. However, afaict, it can't always guess correctly which of the two you want to see.
So, to ensure that a value is rendered as a date, you'd need to open the Format Cells dialog, go to the first tab Number, and set the cell's format Category to one of the Date types.

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: Check whether a cell has text in another cell and with symbol, or trim left and right

So i have cell from long text from formula witch
=RIGHT(J2;15)
2016-07-22 2016
How to take out both 2016- and 2016 using find? It needs to be variable since in 2017 it wont be 2016. Can i find using something like FIND(K2&"-";J2)? Example below.
Result i need to get only 07
I used
=MID(LEFT(J2;FIND(N2;J2)-5);FIND(K2;J2)+5;LEN(J2))
But it finds both of 2016 and gives nothing
Wanted to something like
=MID(LEFT(J2;FIND(N2;J2)-5);FIND(K2&"-";J2)+5;LEN(J2))
But doesnt work
Pulling the left 10 characters off the text string should allow direct conversion to a true date using the DATEVALUE function (possibly assuming the conputer's correct system DMY vs. MDY regional setting). Once that true date has been achieved, a wrapping TEXT function can resolve any part of the date (e.g. year, month or day).
=TEXT(DATEVALUE(LEFT(J2, 10)), "mm") ◄ month as text representing a number
=--TEXT(DATEVALUE(LEFT(J2, 10)), "mm") ◄ month as true number

Date is not converted exactly when read from excel

I have an Excel spreadsheet that is being read.
The value in the spreadsheet is 7/24/2014 10:43:33 AM
The cell value after being read using OpenXML is 41844.446908680555.
When I do this calculation to convert to a date:
dte = DateTime.FromOADate(double.Parse(value));
I get 7/24/2014 10:43:32 AM
Is this typical when converting date/time or am I missing something?
Thanks
It seems that DateTime.FromOADate(double.Parse(value)); is truncating rather than rounding the fractional seconds. Excel stores date/time as days and fractions of days since 1 Jan 1900 (with the intentional error of calling 1900 a leap year, supposedly for compatibility with Lotus 123 at the time).
Therefore, the number 41844.446908680555 translates to, given Excel's level of precision
7/24/2014 10:43:32.910
(actually: 7/24/2014 10:43:32.9099949030205)
Just format the cells as Dates. 41844.446908680555 is Excel's way of serializing the date value.
When Excel stores a date or time in stores it in a number format with the date January 1, 1900 = 1
so really when you’re storing a date with the date format you’re really just storing the numeric value of difference between the date and January 1, 1900
So for example 365 = jan-30-1900
And fractions of a number equal parts of the day so .5 = half a day or 12 hours.
And for the fun of it right now = 41885.75 or sept-3-2014 at 6PM or 41885.75 from jan 1 1900.
The reason why this is done is to now allowed dates to be used in mathematical functions. And it deals with a lot of problems that pop-up with dates such as leap year and also provides for easier ways to deal with time zones as well.

Convert long date text to date / time

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))

.NumberFormat sometimes returns the wrong value with dates and times

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

Resources