Convert Excel date number to datetime in Python - python-3.x

I want to covert a column with Excel date numbers (float) to datetime, below is the function I am trying to use:
import datetime
datetime.datetime.fromtimestamp(42029.0).strftime('%Y-%m-%d')
Bu the result I got is: '1970-01-01',I don't think it is right, I must missing a constant which should be added to the variable, because in the excel the number 42029.0 represents date: 1/25/2015.
Can anyone please advise how to improve the code?

datetime.fromtimestamp() follows the unix convention and expects seconds since '1970-01-01'. The Excel date number for '1970-01-01' is 25569, so you have to subtract this number from your given date number and multiply the result, which is in days, with 86400 (seconds per day):
datetime.datetime.fromtimestamp(
(42029.0 - 25569) * 86400).strftime('%Y-%m-%d')
'2015-01-25'

Related

Converting large time format to decimal in excel

I'm trying to convert a large time value in excel to a decimal number for hours.
I currently have a column adding up "Ready time" for a call centre which is 3545:20:02 as a SUM. I now want that to show me the same hours in a decimal format e.g. 3545.333 as it's used in another calculation.
For reference, when I convert the above time to a General excel value, it is 147.7222454.
The formula I've been using is: =IFERROR((DAY(M54)*24) + HOUR(M54) + (MINUTE(M54)/60),0) and has been working fine for smaller time values.
Thanks in advance!
Excel counts in days (1 day = 1) so for hours you just multiply by 24, i.e.
=M54*24
format result cell as number with required number of decimals
[the reason your current formula fails is because of DAY function - DAY is day of the month so it fails for you when the time value is >= 32*24 = 768 hours]

Time (HH:MM:SS) in Excel is converted to decimal value in openpyxl

I am trying to run the following code :
import openpyxl
wb = openpyxl.load_workbook("/Users/kakkar/Downloads/TRADEBOOK ALL-EQ 01-01-2016 TO 04-02-2017.xlsx")
sheet = wb.get_sheet_by_name('TRADEBOOK')
print(sheet.cell(row=15, column=3).value)
print(sheet.cell(row=15, column=3).internal_value)
In the input Excel file, column 3 contains the time value 10:47:49. This is being treated in openpyxl as the floating-point value 0.449872685185185.
How can I get the time value in the form HH:MM:SS?
Excel stores time (and date) values as floating-point numbers. The whole number is the number of days since the epoch (1 Jan 1970), while the remainder is the portion of the day.
Here is an answer that can help you get the hours, minutes, and seconds out of the value, but this answer might be better for getting the answer without writing code (it uses the xlrd library).
the .internal_value of 10:47:49 (HH:MM:SS) IS 0.449872685185185, so its OK.
Please give as the following output:
cell = sheet['C15']
print('cell=\t%s\n\ttype=%s\n\t.number_format=%s;\n\t.value=%s;\n\t.internal_value=%s;' % (cell,str(type(cell.value)),cell.number_format,cell.value,cell.internal_value) )
Tested with Python:3.4.2 - openpyxl:2.4.1 - LibreOffice: 4.3.3.2

How do I convert this date text to a date field in excel?

I have a date that looks like this text. 17-OCT-16 03.24.11.000000000 AM
I need to format that text into a date that I can then manipulate. Ultimate I want to transform that time to millis from epoch but I'm not sure if that's even possible
Its definitely possible, you can make use of DATEVALUE and TIMEVALUE function which convert a time or date in string format to date/time format:
=DATEVALUE(LEFT(A1,9)) + TIMEVALUE(SUBSTITUTE(MID(A1,11,8), ".", ":")&MID(A1, 19, 9))
This will give you a single number (42660.1417939815) in this case which is the number of days since 1/1/1900 (including milliseconds)
It should just be some simple maths to get total milliseconds

Datetime from number or text in Excel

Id appreciate if someone can help.
I have a column with the following values:
DATETIME
1401032014
1401032014
1401010629
1401032011
1401010629
1401032011
The cell is a number (or can be displayed as text). It shows the datetime. Example:
1401032011:
year: 14
month:01
day:03
hours: 20
minutes: 11
How can I split the value and concatenate into datetime? Notice i have only two last digits of the year - 14, so the full datetime should have the format like this "2014-01-03 20:11"
Thanks a lot in advance.
If you only want the formula to return text use:
="20"&LEFT(A1;2)&"-"&MID(A1;3;2)&"-"&MID(A1;5;2) & " " & MID(A1;7;2) &":"&MID(A1;9;2)
If you want the formula to return a datetime value use:
=DATE(2000+LEFT(A1;2);MID(A1;3;2);MID(A1;5;2))+(MID(A1;7;2)*60+MID(A1;9;2))/1440
And then (for the last one) format the datetime value at will.
I always prefer, when possible, to use date instead of datevalue because datevalue interprets date_text argument based on regional settings and will return an error with different Regional Settings.

Convert milliseconds to date (in Excel)

I have a row in excel with the following data: 1271664970687 (I think it's the number of milliseconds from 1970...).
I would like to have addition row that will show it as date/time.
Converting your value in milliseconds to days is simply (MsValue / 86,400,000)
We can get 1/1/1970 as numeric value by DATE(1970,1,1)
= (MsValueCellReference / 86400000) + DATE(1970,1,1)
Using your value of 1271664970687 and formatting it as dd/mm/yyyy hh:mm:ss gives me a date and time of 19/04/2010 08:16:11
See Converting unix timestamp to excel date-time forum thread.

Resources