How to pass a datetime parameter?

后端 未结 10 1697
暗喜
暗喜 2020-11-29 01:56

How to pass UTC dates to Web API?

Passing 2010-01-01 works fine, but when I pass a UTC date such as 2014-12-31T22:00:00.000Z (with a time c

10条回答
  •  北海茫月
    2020-11-29 02:43

    This is a solution and a model for possible solutions. Use Moment.js in your client to format dates, convert to unix time.

     $scope.startDate.unix()
    

    Setup your route parameters to be long.

    [Route("{startDate:long?}")]
    public async Task Get(long? startDate)
    {
        DateTime? sDate = new DateTime();
    
            if (startDate != null)
            {
                sDate = new DateTime().FromUnixTime(startDate.Value); 
            }
            else
            {
                sDate = null;
            }
             ... your code here!
      }
    

    Create an extension method for Unix time. Unix DateTime Method

提交回复
热议问题