0

I wanted to format the below source date to target format in JQGrid, I tried several formats but not able to do so. Please help. I am getting "01/30/2020"

Source Format : 30-JUN-2020 Target Format : 06/30/2020

Code:

{
  label: '<font color="red" size="3">*</font><font size="2">End Date</font>',
  name: 'EndDate',
  key: false,
  index: 'EndDateHidden',
  editable: true,
  editrules: {
    required: true
  },
  formatter: 'date',
  sorttype: 'date',
  formatoptions: {
    srcformat: 'D-m-Y',
    newformat: 'm/d/Y'
  },
  searchoptions: {
    //sopt: ['eq'],
    placeholder: 'End Date',
    title: 'End Date'
  }
}, {
  name: 'EndDateHidden',
  hidden: true,
  formatter: 'date',
  formatoptions: {
    srcformat: 'm/d/Y',
    newformat: 'm/d/Y'
  }
}

and at last I have added

onInitGrid: function() {
  for (var i = 0, len = this.p.data.length; i < len; i++) {
    var row = this.p.data[i];
    row['conEndDateHidden'] = $.jgrid.parseDate.call(this, 'D-m-Y', row.EndDate, 'm/d/Y');
    console.log(" row['EndDateHidden'] :" + row['EndDateHidden'] + "EndDate :" + row['EndDate']);
  }
}
Ganesh
  • 903
  • 1
  • 8
  • 18

2 Answers2

0

$(document).ready(function(){
GetDateFormated("30-JUN-2020");
})

function GetDateFormated(GivenDate) {
    var GivenMonth = GivenDate.replace(/[0-9]/g, '');
    var UpdatedDate;
    GivenMonth = GivenMonth.replace(/-/g, "");
    var Month = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEPT', 'OCT', 'NOV', 'DEC'];

    for(var i=0;i<12;i++)
    {
        if(Month[i]==GivenMonth)
        {
            if (i.toString().length == 2) {
                UpdatedDate = GivenDate.replace(GivenMonth, (i+1));
            }
            else {
                UpdatedDate = GivenDate.replace(GivenMonth, '0' + parseInt(i + 1).toString());                    
            }
                UpdatedDate = UpdatedDate.replace(/-/g, "/");
                console.log(UpdatedDate)
        }
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Eshu
  • 185
  • 6
0

Try to use

formatoptions: {
    srcformat: 'd-M-Y', // !!! instead of 'D-m-Y'
    newformat: 'm/d/Y'
}
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Tried it, but no luck.. Still getting NaN/NaN/NaN... Just for your information I getting the date as String from Java code to JQGrid.. – Ganesh Jun 22 '18 at 10:42
  • @Ganesh: It's no so important, which language you use on the server side. It's important only the exact format of JSON/XML data returned from the server. Do you verified with Developer Tools (see Network tab), which format has the date really? Additionally it could be very important to know which **version** of jqGrid you use (can use) and from which **fork** of jqGrid ([free jqGrid](https://github.com/free-jqgrid/jqGrid), commercial [Gruriddo jqGrid JS](http://guriddo.net/?page_id=103334) or an old jqGrid in version <=4.7). The problem could exist if you use some retro version of jqGrid. – Oleg Jun 22 '18 at 10:47
  • @Ganesh: One more remark: if you use the format, which I suggested you don't need any `onInitGrid`, which convert the input data. Do you removed it in your tests? Do you use `datatype: "local"` and create grid with `data` parameter? – Oleg Jun 22 '18 at 10:49
  • Thanks @Oleg, I checked the date in network tab I getting the "30-JUN-2020" only and as you said removed teh onInitGrid and applied the given format with little bit change as "d-MM-Y" then I am getting "01-30-2020" that means the date and year is coming correct but month is wrong. And using jqGrid 5.0.1. Please help – Ganesh Jun 22 '18 at 12:14
  • @Ganesh: Sorry, but I can't follow you. You wrote "the given format with little bit change as "d-MM-Y""? Do you changed the format which returns the server and the server returns "01-30-2020" instead of "30-JUN-2020"? In the case you have to change `srcformat` property of `formatoptions` corresponds to your last changes. If you can change the server code then why you not just use the *standard* ISO format "2020-01-30"? In the case you should remove `srcformat` property of `formatoptions`. – Oleg Jun 22 '18 at 12:30
  • @Ganesh: "jqGrid" exist till version 4.7. After that: "free jqGrid" and "Guriddo jqGrid JS". If you use 5.0.1 then you use **commercial** [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334), which can't be used for free. I develop the fork [free jqGrid](https://github.com/free-jqgrid/jqGrid), which can be used completely free of charge. – Oleg Jun 22 '18 at 12:31