Bootstrap-table: Sorting by Date in Correct Format

Created on 17 Oct 2015  Â·  6Comments  Â·  Source: wenzhixin/bootstrap-table

I just need to sort my table by the values in a date colume. Currently, I'm echoing the dates in YYYY-MM-DD, but I can echo the date values in whatever format needed, but ultimately want them displayed in MM/DD/YYYY or M/D/YYYY in Bootstrap Table. Is there a JSFiddle anywhere for how I would go about sorting dates in one of those two formats?

Most helpful comment

So I ended up placing the <span class="hidden">YYYY-MM-DD</span> before echoing the desired format. Seems hacky but works. Curious as to what the correct way to do this is.

All 6 comments

So I ended up placing the <span class="hidden">YYYY-MM-DD</span> before echoing the desired format. Seems hacky but works. Curious as to what the correct way to do this is.

Format the date field as you retrieve it from your table like this...$sql =
"SELECT id,DATE_FORMAT(Rec_Date, '%m/%d/%Y') as mdy_Date, etc...from
where ...". Rec_Date is the field in the table where the date
is stored in the normal mysql/oracle date format. For presentation I
formatted it on the select and the sort works fine. I used the name
mdy_Date to remind me what format I was going for. The names are not
written in stone.

This technique works on all the platforms that I have used. Too many years
of sql programming.

On 16 October 2015 at 16:10, Chase Giunta [email protected] wrote:

So I ended up placing the before
echoing the desired format. Seems hacky but works. Curious as to what the
correct way to do this is.

—
Reply to this email directly or view it on GitHub
https://github.com/wenzhixin/bootstrap-table/issues/1563#issuecomment-148862662
.

@kct3937 I don't believe you understood the question. I can output whatever format needed.

By default, columns are sorted alphabetically by the content in the cell (which may include HTML formatting).

You can specify a custom JavaScript sorter function for a column with your dates in it.

Check out http://issues.wenzhixin.net.cn/bootstrap-table/#options/custom-sort.html

and https://github.com/wenzhixin/bootstrap-table-examples/blob/master/options/custom-sort.html for an example of using acustom sorter

The sorter function take two cell values to compare: mySorter(a, b). The function returns either 1, 0 or -1 depending on the comparison of a and b. In our case you would need to convert the formatted date into a format that can be compared in the way you want. (maybe turn the a and b values into JavaScript date objects or a javascript integer timestamp).

@tmorehouse I appreciate the answer! Yeah I had seen the custom sort options. Unfortunately, I'm not fluent, by any means, in javascript. So I was hoping I would be able to stumble upon a snippet or JSFiddle of something as common as sorting by date.

So I ended up placing the <span class="hidden">YYYY-MM-DD</span> before echoing the desired format. Seems hacky but works. Curious as to what the correct way to do this is.

this didn't work for me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marcelod picture marcelod  Â·  16Comments

loveleshsaxena picture loveleshsaxena  Â·  20Comments

antonioaltamura picture antonioaltamura  Â·  15Comments

DavidKrupi picture DavidKrupi  Â·  23Comments

wenzhixin picture wenzhixin  Â·  35Comments