PowerShell and DateTime


i create type:

          $authticketrepresentationsource = @"  using system;    namespace sonicfoundry.mediasite.webapi.model  {      public class authorizationticketrepresentation`      {`          public string ticketid { get; set; }          public string username { get; set; }          public string clientipaddress { get; set; }          public string owner { get; set; }          public datetime creationtime { get; set; }          public datetime expirationtime { get; set; }          public string resourceid { get; set; }          public int minutestolive { get; set; }      }  }  "@            add-type -typedefinition $authticketrepresentationsource  

i create instance , populate ad convert json string:

        $poststr = [system.text.encoding]::utf8.getbytes($jsonauthticketrepresentation.tostring())

then post data url. problem dates come out as:


{
    "ticketid":  "00000000-0000-0000-0000-000000000000",
    "username":  "kevinb",
    "clientipaddress":  "10.0.70.154",
    "owner":  "",
    "creationtime":  "\/date(1379610208888)\/",
    "expirationtime":  "\/date(-62135575200000)\/",
    "resourceid":  "773820afc2004c8dad7b8c023201dec218",
    "minutestolive":  5
}

rather than

{  "ticketid":"00000000-0000-0000-0000-000000000000",  "username":"kevinb",  "clientipaddress":"10.0.70.154",  "owner":"",  "creationtime":"2013-09-19t12:34:22.0987775-05:00",  "expirationtime":"0001-01-01t00:00:00",  "resourceid":"599bc68366fd407a81c004e9ca91147a1d",  "minutestolive":5  }

this causing deserialization issues server data being posted to. how can latter serialized representation of dates?

thank you.


kevin burton

hi kevin,

this default serialization dates in json represents milliseconds since beginning of unix epoch. 

(new-object datetime (1970,1,1)).addmilliseconds(1379610208888)

the workaround use string property or call properties tostring method before converting json:

$test = new-object sonicfoundry.mediasite.webapi.model.authorizationticketrepresentation $test.creationtime=get-date $test.expirationtime=$test.creationtime.adddays(5) #modify according stringformat you'd $test | select @{n="creationtime";e={$_.creationtime.tostring("s")}},@{n="expirationtime";e={$_.expirationtime.tostring("s")}} | convertto-json 



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Windows 2016 RDS event 1306 Connection Broker Client failed to redirect the user... Error: NULL