3

I am using jqGrid for displaying records in a user interface, and I want to sort the date column. Which returns me the date in the form of Sat, Sep 1, 01:41 AM. How can I do that with jqGrid?

My Colmodel for that column is:

{name:'transactiontime', index:'transactiontime',  sorttype:'text', align:"right"},
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86

2 Answers2

5

Try the following formatoptions:

{name: 'transactiontime', sorttype: 'date', align: "right", formatter: 'date',
    formatoptions: {srcformat: 'ISO8601Long', newformat: 'D, M d, H:i A'}}

See the demo which is a simple modification of the demo from the answer.

The srcformat could depend on the input format of your data.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • What is `ISO8601Long` format and how to pass that value from server side ? – Bhavik Ambani Sep 18 '12 at 04:46
  • @BhavikAmbani: `ISO8601Long` is the same as `"Y-m-d H:i:s"` (see [the line](https://github.com/tonytomov/jqGrid/blob/v4.4.1/js/i18n/grid.locale-en.js#L114)). It's [ISO8601](http://en.wikipedia.org/wiki/ISO_8601) format which includes the time information. Examples from my demo: `"2007-10-01T16:48:14"` and `"2007-10-01T16:47"`. One can use `2012-09-17T09:49Z` to specify UTC time – Oleg Sep 18 '12 at 05:15
  • What if I want to sort the values like `750.46 KB` or `750.46 MB` etc. ? Please respond as soon as possible. – Bhavik Ambani Sep 19 '12 at 13:02
  • 2
    @BhavikAmbani: I don't like if one write me "respond as soon as possible". I am now in another country at the customer who pay me for the consulting work which I do (no relation with jqGrid). So I can't make support other people over the world. Nevertheless what you need is probably implementing of **custom sorting**: the usage `sorttype` as function. See the demo from [the answer](http://stackoverflow.com/a/5296935/315935). – Oleg Sep 19 '12 at 13:20
  • Still please responde to my question if possible. – Bhavik Ambani Sep 19 '12 at 13:29
  • @BhavikAmbani: I answered already in the previous comment: you can implements *custom sorting* for the column having 750.46 KB: see [the answer](http://stackoverflow.com/a/5296935/315935). In the demo from the answer the `parseInt` will be called to parse the integer part of the value. You can convert `750.46 KB` or `750.46 MB` in the same way to the number which represent **bytes** and return the number from `sorttype` function. – Oleg Sep 19 '12 at 14:16
  • @BhavikAmbani: I'm busy now. I think you can write yourself the JavaScript code which convert the string `"750.46 KB"` to the number `76847104` (`750.46*1024`) and which comvert the string `"750.46 MB"` to the number `78691434496` (`750.46*1024*1024`). – Oleg Sep 19 '12 at 14:50
  • My problem is just related with sorting of the values in the JQgrid. – Bhavik Ambani Sep 20 '12 at 04:27
  • One more thing is that the `newformat: 'D, M d, H:i A'}` is not working with chrome. It displays ambiguous format of dates. – Bhavik Ambani Sep 24 '12 at 04:35
  • Hi, I am formatting date, which has Day Name and date like , Wed, 01-03-2017, But some how it behave weird, I have saved the fiddle please have a look http://jsfiddle.net/alpeshjikadra/jss5b43j/1/ – Alpesh Jikadra Mar 29 '17 at 07:34
  • @AlpeshJikadra: You should post **separate** question, where you describe detailed the problem. You should mention, which version of jqGrid you use (can use) and from which fork of jqGrid: [free jqGrid](https://github.com/free-jqgrid/jqGrid), commercial [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or an old jqGrid in version <=4.7. – Oleg Mar 29 '17 at 08:18
0

Use sorttype:'date' and datefmt:

datefmt governs the format of sorttype:date (when datetype is set to local) and editrules {date:true} fields. It determines the expected date format for that column and uses a PHP-like date formatting. Currently ”/”, ”-”, and ”.” are supported as date separators. Valid formats are:

  • y,Y,yyyy for four digits year
  • YY, yy for two digits year
  • m,mm for months
  • d,dd for days.

Of course, you can sort on server side too, if it is more painless. You can use the sidx and sord fields on server side.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zbacsi
  • 149
  • 1
  • 4