본문 바로가기
Language & Library/jqGrid

jqGrid 날짜(date: yyyymmdd) 관련

by 댓츠굿 2013. 12. 30.


- 참조사이트(jqGrid Wiki) : http://www.trirand.com/jqgridwiki/doku.php?id=wiki:predefined_formatter

- 해결책을 준 참조사이트: http://stackoverflow.com/questions/12803469/jqgrid-date-formatter-ymd




※ 문제(problem)

I have a problem with the jqgrid Date formatter. in my DB table i have this dateformat "Ymd" like "20120219". Here the whole code for this column:

$col = array();
$col["title"] = "Date";
$col["name"] = "date_dem";
$col["width"] = "40";
$col["editable"] = false;
$col["editoptions"] = array("size"=>20);
$col["formatter"] = "date"; // format as date
$col["formatoptions"] = array("srcformat"=>'Ymd',"newformat"=>'d/m/Y');
$cols[] = $col;

When i use this code i get for every record the same output: "01/01/1970". Anybody knows this problem?

THX in advanced



※ 해결책(solution)

Even if the information about the date are saved as Ymd without any separators you have to return from the server the information with some separator between year, month and day. The problem is that the current code of jqGrid first split the input date using '\''/'':''_'';''.' or ',' as separator (see the line of code)

date = String(date).split(/[\\\/:_;.,\t\T\s-]/);

and then use the srcformat to decode it. So you can't use formatter: "date" to decode the date like 20121009. I recommend you to convert 20121009 to 2012-10-09 on the server side and return from the server the date always in ISO 8601 format which is defalt format used by jqGrid. Alternatively you can use custom formatter to display the date.




※ 요약(summary)

서버 혹은 디비에서 yyyymmdd( ex)20130101 ) 형식의 데이터를 보내는데, jqGrid 상의 모든 날짜가 1970-01-01 로 나왔다. 

알고보니, 위와 같은 형식은 decode 하지 못한단다.( ISO 8601 format 이 아닌가 보다.)

그래서 서버에서 yyyymmdd 형식의 데이터를 yyyy-mm-dd 형식으로 바꿔줘서 데이터를 줬더니 jqGrid에서 formatter가 date인 데이터를 마음데로 조작할 수 있었다.




반응형