【问题标题】:JqGrid datetime column returns string value like /Date(1380565800000) instead of DateTime objectJqG​​rid datetime 列返回字符串值,例如 /Date(1380565800000) 而不是 DateTime 对象
【发布时间】:2013-10-18 00:03:48
【问题描述】:

我正在使用具有以下 ColModel 定义的 jqGrid

colModel: [
        .
        .
        .
        {
            name: 'ReadingTransferTime', index: 'ReadingTransferTime', width: 78, formatter: 'date', formatoptions: { srcformat: 'm/d/Y', newformat: 'm/d/Y' },
            sorttype: 'date', fixed: true, align: 'center'
        },
        .
        .
        .
        {
            name: 'CPAPStatus', index: 'CPAPStatus', sortable: false, align: 'center', formatter: LoadCPAPFollowUpDialog, width: 100, fixed: true,
            cellattr: function (rowId, tv, rawObject, cm, rdata) { return 'style="white-space: normal!important;' }
        },            
        { name: 'CPAPDeliveryReason', hidden: true },
        { name: 'CPAPDeliveredDate', hidden: true },
        { name: 'CPAPDeliveryStatus', hidden: true }
        ],

自定义格式化程序LoadCPPFollowUpDialog如下

function LoadCPAPFollowUpDialog(cellvalue, options, rowObject) {        
var paramList = JSON.stringify({
    ReadingID: rowObject.ReadingID,
    TransferTime:rowObject.ReadingTransferTime,
    PatientName: rowObject.PatientFullName,
    PAPDeliveredDate: rowObject.CPAPDeliveredDate,
    NonDeliveryReason: rowObject.CPAPDeliveryReason,
    GridID: "HSTCandidatesDtls"
});

return "<img src='../Content/images/icons/edit.gif' title='" + "@VirtuOxAdmin.SleepStudyDetails_Image_CPAPStatus" + "' \
onClick='openDialog(\"SleepStudyDtlsDialog\",\"" + "@VirtuOxAdmin.SleepStudyDetails_Dialog_CPAPStatus" + "\",\"CPAPDelivery\"," + paramList + ",\"500\",\"auto\")'>" + "<span>" + rowObject.CPAPDeliveryStatus + "</span>";

}

在此列值 ReadingTransferTime 和 CPAPDeliveredDate 的格式化程序中,我得到的是 /Date(1380565800000)/ 之类的字符串值,而不是 datetime 对象。这给我的操作方法 CPAPDelivery 接受错误的参数值造成了问题。如何解决这个问题?

我从here 得到了 1 个解决方案 & 将我的 paramList json 对象形成为

var paramList = JSON.stringify({
    ReadingID: rowObject.ReadingID,
    TransferTime:new Date(rowObject.ReadingTransferTime.match(/\d+/)[0]*1),
    PatientName: rowObject.PatientFullName,
    PAPDeliveredDate: (rowObject.CPAPDeliveredDate!=null? new Date(rowObject.CPAPDeliveredDate.match(/\d+/)[0]*1):null),
    NonDeliveryReason: rowObject.CPAPDeliveryReason,
    GridID: "HSTCandidatesDtls"
});

这是解决上述问题的正确方法吗?或者 jqGrid 有一些内置的功能来处理它。

【问题讨论】:

    标签: jquery asp.net-mvc-4 jqgrid


    【解决方案1】:

    试试这个,

     string jsonDate = "/Date(1380565800000)/";
     jsonDate = jsonDate.Replace("/Date(", string.Empty); 
     jsonDate = jsonDate.Replace(")/", string.Empty);
    
     var date = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddMilliseconds(long.Parse(jsonDate));
     date = date.AddDays(1);
    

    希望这会有所帮助。

    【讨论】:

    • 您好,感谢您的建议和帮助。我用来解决问题的方法运行良好;但我不确定我迄今为止解析字符串的方式在某些情况下不会引起任何问题。如果您知道我的日期解析可能会失败的某些情况,请告诉我同样的情况。
    猜你喜欢
    • 2019-10-29
    • 2021-07-30
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 2015-12-02
    相关资源
    最近更新 更多