# Datatable datetime format

**URL:** <https://community.plotly.com/t/datatable-datetime-format/31091>\
**Category:** Dash Python\
**Created:** [November 9, 2019, 2:08am UTC](https://community.plotly.com/t/datatable-datetime-format/31091 "2019-11-09T02:08:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![PBK](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@PBK](https://community.plotly.com/u/PBK)\
**Post date:** [November 9, 2019, 2:08am UTC](https://community.plotly.com/t/datatable-datetime-format/31091/1 "2019-11-09T02:08:17Z")

</div>

I have data from a pandas dataframe that I want to put in a datatable. The first field is a date. It’s formatted YYYY-MM-DD in pandas. In the dataframe it’s format is YYYY-MM-DDT00:00:00. How do I get time out of the format in the dataframe? In all of the examples in the DataTable user guide dates are in YYYY-MM-DD format.

This worked properly in an earlier version of Dash and DataTable (I believe the version of Dash was 0.4).

---

<div class="post-metadata">

**Author:** ![Falc](https://avatars.discourse-cdn.com/v4/letter/f/b4bc9f/32.png) [@Falc](https://community.plotly.com/u/Falc)\
**Post date:** [December 8, 2019, 5:43pm UTC](https://community.plotly.com/t/datatable-datetime-format/31091/2 "2019-12-08T17:43:20Z")

</div>

I’m also having this issue. Anyone found a fix?

---

<div class="post-metadata">

**Author:** ![Gingey](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gingey](https://community.plotly.com/u/Gingey)\
**Post date:** [December 12, 2019, 8:21pm UTC](https://community.plotly.com/t/datatable-datetime-format/31091/3 "2019-12-12T20:21:20Z")

</div>

I hit all my date columns with this before populating the DataTable component:

```auto
for x in ["Date Col1", "Date Col2", "Date Col3"]:
        if x in df.columns:
            df[x] = pd.to_datetime(df[x], format="%Y-%m-%d").dt.floor("d")

```

---

<div class="post-metadata">

**Author:** ![Falc](https://avatars.discourse-cdn.com/v4/letter/f/b4bc9f/32.png) [@Falc](https://community.plotly.com/u/Falc)\
**Post date:** [January 7, 2020, 10:01am UTC](https://community.plotly.com/t/datatable-datetime-format/31091/4 "2020-01-07T10:01:48Z")

</div>

That doesn’t work for me unfortunately, Dash still adds the extra ‘00:00:00’.

---

<div class="post-metadata">

**Author:** ![marlon](https://avatars.discourse-cdn.com/v4/letter/m/8c91f0/32.png) [@marlon](https://community.plotly.com/u/marlon)\
**Post date:** [January 14, 2020, 1:41pm UTC](https://community.plotly.com/t/datatable-datetime-format/31091/5 "2020-01-14T13:41:28Z")

</div>

Assuming that your column is named as “Date\_Col”, you can format it before creating the data table:

```auto
df.Date_Col = pd.DatetimeIndex(df.Date_Col).strftime("%Y-%m-%d")

```

That should do it.

Also refer to this documentation for further details [https://dash.plot.ly/datatable/typing](https://dash.plot.ly/datatable/typing).

---

<div class="post-metadata">

**Author:** ![Falc](https://avatars.discourse-cdn.com/v4/letter/f/b4bc9f/32.png) [@Falc](https://community.plotly.com/u/Falc)\
**Post date:** [January 17, 2020, 7:12pm UTC](https://community.plotly.com/t/datatable-datetime-format/31091/6 "2020-01-17T19:12:37Z")

</div>

Thanks that worked! I’ll check out that documentation

---

<div class="post-metadata">

**Author:** ![ozan](https://avatars.discourse-cdn.com/v4/letter/o/f4b2a3/32.png) [@ozan](https://community.plotly.com/u/ozan)\
**Post date:** [May 31, 2020, 4:23pm UTC](https://community.plotly.com/t/datatable-datetime-format/31091/7 "2020-05-31T16:23:15Z")

</div>

Sorting doesn’t work since strftime converts to string

---

<div class="post-metadata">

**Author:** ![OnkelNuun](https://avatars.discourse-cdn.com/v4/letter/o/dfb087/32.png) [@OnkelNuun](https://community.plotly.com/u/OnkelNuun)\
**Post date:** [June 3, 2020, 3:12pm UTC](https://community.plotly.com/t/datatable-datetime-format/31091/8 "2020-06-03T15:12:11Z")

</div>

I haven´t yet found a ‘format’ solution in the table that allows to sort by date.

Workaround:  
I used the index numbers of the df (sorted by datetime) as an “id” column in the table to sort by date.

---

<div class="post-metadata">

**Author:** ![zachmorris](https://avatars.discourse-cdn.com/v4/letter/z/858c86/32.png) [@zachmorris](https://community.plotly.com/u/zachmorris)\
**Post date:** [August 11, 2020, 10:00pm UTC](https://community.plotly.com/t/datatable-datetime-format/31091/9 "2020-08-11T22:00:22Z")

</div>

Just bumping this topic. I’m not understanding how I can format a datetime column while still allowing it to be sorted correctly.

Something like:

```auto
columns=[{'id':'Date','name':'My Formatted Date','type': 'datetime', 'format:'???'}]

```

But I’m not understanding what that ??? should be.

---

<div class="post-metadata">

**Author:** ![saufrec](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@saufrec](https://community.plotly.com/u/saufrec)\
**Post date:** [August 26, 2020, 9:01pm UTC](https://community.plotly.com/t/datatable-datetime-format/31091/10 "2020-08-26T21:01:56Z")

</div>

According to [https://dash.plotly.com/datatable/reference](https://dash.plotly.com/datatable/reference), columns.format is “derived from the [d3-format](https://github.com/d3/d3-format) library specification”, which doesn’t have any date-formatting functionality. So that definitely won’t work. If you change the date column of the DataFrame into a string with the format YYYY-MM-DD, before you send the DataFrame to the Datatable, then it will display correctly and sort correctly, because alphabetical order on the string is the same as chronological order.

---

<div class="post-metadata">

**Author:** ![danpryjma](https://avatars.discourse-cdn.com/v4/letter/d/ee59a6/32.png) [@danpryjma](https://community.plotly.com/u/danpryjma)\
**Post date:** [February 15, 2021, 10:00am UTC](https://community.plotly.com/t/datatable-datetime-format/31091/11 "2021-02-15T10:00:13Z")

</div>

For me the way it works best is to convert the data in pandas to dt.date format:  
df[date\_col] = df[date\_col].dt.date

When you move this to datatable it keeps just YYYY-MM-DD with no time and it still is a date.
