Jqgrid of time formatting problems

Source: Internet
Author: User
Tags jqgrid

In the project, the front desk Jqgrid in the display of time, there are some problems, find out after the record to meet the same problem people

in the project, the JSON-formatted data was written to Jqgrid, and the data format was converted by Jackson in the background. Jagrid Time Line code is as follows
Java code
  1. colmodel:[
  2. {name: 'ID', index:' id ', editable:false, Hidden:True},
  3. {name:' name ', index:' name ', Label: 'role name', SortType:' text '},
  4. {name:' createtime ', index:' createtime ', Label: 'creation time ', editable:false,formatter:"Date", Formatoptions: {srcformat:' y-m-d h:i:s ', Newformat:' y-m-d h:i:s '}},
  5. {name:' CreateBy ', index:' CreateBy ', Label: 'Creator ', sortable:false}
  6.             ],  


but in the creation time that line of time shows as "Nan-nan-nan NaN:NaN:NaN", and later only, Jaskson the default time format is time stamp, in Jqgrid, with "U" or "U" to represent the source data timestamp format, So change the above code to read as follows:
Java code
  1. colmodel:[
  2. {name: 'ID', index:' id ', editable:false, Hidden:True},
  3. {name:' name ', index:' name ', Label: 'role name', SortType:' text '},
  4. {name:' createtime ', index:' createtime ', Label: 'creation time ', editable:false,formatter:"Date", Formatoptions: {srcformat:' u ', Newformat:' y-m-d h:i:s '}},
  5. {name:' CreateBy ', index:' CreateBy ', Label: 'Creator ', sortable:false} /c0>
  6.             ],  


after the change, the number is out, but the display time is too outrageous, shown as: "44235-11-07 21:10:44". Again find information, just know, Jqgrid mainly support this PHP, and PHP and Java in time stamp difference, PHP inside, timestamp with 10 digits, accurate to seconds, Java inside, timestamp with 13 digits, accurate to milliseconds. To view the Jqgrid source code has a paragraph as follows:


Java code
    1. If(!isnan (Date- 0 ) && String (format). toLowerCase () = = "U") {
    2. //unix timestamp
    3. timestamp = new date (parsefloat (date) * );
    4. }   


look again, do not see other processing time stamp place, tracking JS code, for from the background to get the 13-bit timestamp data will also enter here, multiplied by 1000 on the programming of 16, which led to a lot larger than the original time. This paragraph is amended as follows

Java code
    1. If(!isnan (Date- 0 ) && String (format). toLowerCase () = = "U") {
    2. //unix timestamp
    3. timestamp = new date (parsefloat (date));
    4. }   


after the modification, everything appears normal, such as: "2012-04-07 18:13:00",
but think, can not solve the problem so, it means that time to change Jqgrid JS file when the total change this place, modify others source code, still do not know what other problems will appear? Know the problem, then you can find a solution, mainly in which piece of the hand

unexpectedly Jagrid The timestamp has such processing, for Jaskson is used, our project is encapsulated in a tool class inside. Well, I don't give the front desk time stamp data, just as Jackson also provides a way to modify the time data output format, so add a static code, as follows:
Java code
  1. private static objectmapper mapper = new objectmapper ();
  2.   
  3. static {
  4. //jackson The time data written by default is a timestamp, which is modified to the time data output format of the corresponding mode
  5. mapper.getserializationconfig (). Setdateformat (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"))  ;
  6. }  


The front page code is as follows:

Java code
  1. colmodel:[
  2. {name: 'ID', index:' id ', editable:false, Hidden:True},
  3. {name:' name ', index:' name ', Label: 'role name', SortType:' text '},
  4. {name:' createtime ', index:' createtime ', Label: 'creation time ', editable:false,formatter:"Date", Formatoptions: {srcformat:' y-m-d h:i:s ', Newformat:' y-m-d h:i:s '}},
  5. {name:' CreateBy ', index:' CreateBy ', Label: 'Creator ', sortable:false}
  6.             ],  


So after time shows everything normal "2012-04-07 18:13:00"

Jqgrid of time formatting problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.