Convert DateTime To JSON DateTime

前端 未结 7 1115

I have a WebService which return DateTime Field.

I get a result /Date(1379048144000)/ but

i want just 1379048144000 how can i ach

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 22:04

    You could change your WS to return a long with the value of the DateTime. The value to return is the number of milliseconds since the Unix Epoch (01/01/1970). This could be done with an extension method on DateTime something like:

    public static class DateTimeExtensions
    {
        ...
        private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1);
    
        public static long ToUnixTime(this DateTime dateTime)
        {
            return (dateTime - UnixEpoch).Ticks / TimeSpan.TicksPerMillisecond;
        }
        ...
    }
    

    And your web service method might look something like:

    public long GetMyDate(...)
    {
        DateTime dateTime = ...;
        return dateTime.ToUnixTime();
    }
    

提交回复
热议问题