DataTables example Ordering formatted dates (Moment.js)

You might wish to tell DataTables about a specific date/time format that your data contains, but either you don't wish to transform it into a different format, or you aren't sure which columns the date/time data will be in (i.e. can't use the datetime rendering function). For such cases, we provide a DataTable.datetime() function to which you provide a format string. This will instruct DataTables to look for and parse data in such a format.

As with DataTable's other date and time methods, DataTable.datetime() supports both Moment.js and Luxon. In this example we are using Moment.js. For a full range of its formatting options, please see the Moment.js documentation. Note that the parsing is strict - your assigned format must exactly match the data.

NamePositionOfficeAgeStart dateSalary
Tiger NixonSystem ArchitectEdinburgh6125 Apr 2011$320,800
Garrett WintersAccountantTokyo6325 Jul 2011$170,750
Ashton CoxJunior Technical AuthorSan Francisco6612 Jan 2009$86,000
Cedric KellySenior Javascript DeveloperEdinburgh2229 Mar 2012$433,060
Airi SatouAccountantTokyo3328 Nov 2008$162,700
Brielle WilliamsonIntegration SpecialistNew York612 Dec 2012$372,000
Herrod ChandlerSales AssistantSan Francisco596 Aug 2012$137,500
Rhona DavidsonIntegration SpecialistTokyo5514 Oct 2010$327,900
Colleen HurstJavascript DeveloperSan Francisco3915 Sep 2009$205,500
Sonya FrostSoftware EngineerEdinburgh2313 Dec 2008$103,600
Jena GainesOffice ManagerLondon3019 Dec 2008$90,560
Quinn FlynnSupport LeadEdinburgh223 Mar 2013$342,000
Charde MarshallRegional DirectorSan Francisco3616 Oct 2008$470,600
Haley KennedySenior Marketing DesignerLondon4318 Dec 2012$313,500
Tatyana FitzpatrickRegional DirectorLondon1917 Mar 2010$385,750
Michael SilvaMarketing DesignerLondon6627 Nov 2012$198,500
Paul ByrdChief Financial Officer (CFO)New York649 Jun 2010$725,000
Gloria LittleSystems AdministratorNew York5910 Apr 2009$237,500
Bradley GreerSoftware EngineerLondon4113 Oct 2012$132,000
Dai RiosPersonnel LeadEdinburgh3526 Sep 2012$217,500
Jenette CaldwellDevelopment LeadNew York303 Sep 2011$345,000
Yuri BerryChief Marketing Officer (CMO)New York4025 Jun 2009$675,000
Caesar VancePre-Sales SupportNew York2112 Dec 2011$106,450
Doris WilderSales AssistantSydney2320 Sep 2010$85,600
Angelica RamosChief Executive Officer (CEO)London479 Oct 2009$1,200,000
Gavin JoyceDeveloperEdinburgh4222 Dec 2010$92,575
Jennifer ChangRegional DirectorSingapore2814 Nov 2010$357,650
Brenden WagnerSoftware EngineerSan Francisco287 Jun 2011$206,850
Fiona GreenChief Operating Officer (COO)San Francisco4811 Mar 2010$850,000
Shou ItouRegional MarketingTokyo2014 Aug 2011$163,000
Michelle HouseIntegration SpecialistSydney372 Jun 2011$95,400
Suki BurksDeveloperLondon5322 Oct 2009$114,500
Prescott BartlettTechnical AuthorLondon277 May 2011$145,000
Gavin CortezTeam LeaderSan Francisco2226 Oct 2008$235,500
Martena MccrayPost-Sales supportEdinburgh469 Mar 2011$324,050
Unity ButlerMarketing DesignerSan Francisco479 Dec 2009$85,675
Howard HatfieldOffice ManagerSan Francisco5116 Dec 2008$164,500
Hope FuentesSecretarySan Francisco4112 Feb 2010$109,850
Vivian HarrellFinancial ControllerSan Francisco6214 Feb 2009$452,500
Timothy MooneyOffice ManagerLondon3711 Dec 2008$136,200
Jackson BradshawDirectorNew York6526 Sep 2008$645,750
Olivia LiangSupport EngineerSingapore643 Feb 2011$234,500
Bruno NashSoftware EngineerLondon383 May 2011$163,500
Sakura YamamotoSupport EngineerTokyo3719 Aug 2009$139,575
Thor WaltonDeveloperNew York6111 Aug 2013$98,540
Finn CamachoSupport EngineerSan Francisco477 Jul 2009$87,500
Serge BaldwinData CoordinatorSingapore649 Apr 2012$138,575
Zenaida FrankSoftware EngineerNew York634 Jan 2010$125,250
Zorita SerranoSoftware EngineerSan Francisco561 Jun 2012$115,000
Jennifer AcostaJunior Javascript DeveloperEdinburgh431 Feb 2013$75,650
Cara StevensSales AssistantNew York466 Dec 2011$145,600
Hermione ButlerRegional DirectorLondon4721 Mar 2011$356,250
Lael GreerSystems AdministratorLondon2127 Feb 2009$103,500
Jonas AlexanderDeveloperSan Francisco3014 Jul 2010$86,500
Shad DeckerRegional DirectorEdinburgh5113 Nov 2008$183,000
Michael BruceJavascript DeveloperSingapore2927 Jun 2011$183,000
Donna SniderCustomer SupportNew York2725 Jan 2011$112,000
NamePositionOfficeAgeStart dateSalary

The Javascript shown below is used to initialise the table shown in this example:

DataTable.datetime('D MMM YYYY'); $('#example').DataTable();
DataTable.datetime('D MMM YYYY'); new DataTable('#example');

In addition to the above code, the following Javascript library files are loaded for use in this example:

The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:

This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The additional CSS used is shown below:

The following CSS library files are loaded for use in this example to provide the styling of the table:

This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is loaded.

The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side processing scripts can be written in any language, using the protocol described in the DataTables documentation.