How to get Web API OData v4 to use DateTime

前端 未结 12 1213
时光取名叫无心
时光取名叫无心 2020-12-02 14:50

I have a fairly large data model that I want to expose using Web API OData using the OData V4 protocol.

The underlying data is stored in a SQL Server 2012 database.

12条回答
  •  一生所求
    2020-12-02 15:08

    Since i use a library for odata with angular, i investigated it there:

    https://github.com/devnixs/ODataAngularResources/blob/master/src/odatavalue.js
    

    There you can see ( javascript)

    var generateDate = function(date,isOdataV4){
            if(!isOdataV4){
                return "datetime'" + date.getFullYear() + "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "T" + ("0" + date.getHours()).slice(-2) + ":" + ("0" + date.getMinutes()).slice(-2)+':'+("0" + date.getSeconds()).slice(-2) + "'";
            }else{
                return date.getFullYear() + "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "T" + ("0" + date.getHours()).slice(-2) + ":" + ("0" + date.getMinutes()).slice(-2)+':'+("0" + date.getSeconds()).slice(-2) + "Z";
            }
        };
    

    And the test expects ( cfr. here )

     $httpBackend.expectGET("/user(1)?$filter=date eq 2015-07-28T10:23:00Z")
    

    So this should be your "formatting"

    2015-07-28T10:23:00Z
    

提交回复
热议问题