Ashish  Kumar Saxena
Ashish Kumar Saxena

Reputation: 4710

how to show date and datetime field value in excel file?

i am using django framework. my table structure and their values

 1. creation_date       DATE            NOT NULL,     
 2. creation_dt         DATETIME        NOT NULL,
 3. update_dt           DATETIME        NOT NULL

2012-12-12 (creation_date)
2012-12-12 22:54:45.786000 (creation_dt)     
2012-12-12 23:18:15.869000 (update_dt)

after storing data in excel file its showing like that

 1. 41255  (creation_date)
 2. 41255.9546875 (creation_dt)
 3. 41255.9710069444 (update_dt)

Upvotes: 1

Views: 289

Answers (2)

iMom0
iMom0

Reputation: 12921

If you are using xlwt to store them to excel, you should provide extra style to cell:

styles = {
    'datetime': xlwt.easyxf(num_format_str='yyyy-mm-dd hh:mm:ss'),
    'date': xlwt.easyxf(num_format_str='yyyy-mm-dd')
}

Then check the type of value:

if isinstance(value, datetime.datetime):
    cell_style = styles['datetime']
elif:
    cell_style = styles['date']

Write then to cell:

sheet.write(row, col, value, style=cell_style)

Upvotes: 1

Chandan
Chandan

Reputation: 766

Open you excel file. Select column and then right click on column. Select Format Cells... > Number (tab) > Category > Date. Set appropriate date format.

If you are looking for a way to do it through Python then you should also mention the file-format which you are using (and library. Assuming, its not csv)

Right click menu Date format

Upvotes: 0

Related Questions