How can I format DateTime to web UTC format?

前端 未结 8 1927
梦毁少年i
梦毁少年i 2020-11-29 22:45

I have a DateTime which I want to format to \"2009-09-01T00:00:00.000Z\", but the following code gives me \"2009-09-01T00:00:00.000+01:00\" (both l

8条回答
  •  眼角桃花
    2020-11-29 23:27

    You want to use DateTimeOffset class.

    var date = new DateTimeOffset(2009, 9, 1, 0, 0, 0, 0, new TimeSpan(0L));
    var stringDate = date.ToString("u");
    

    sorry I missed your original formatting with the miliseconds

    var stringDate = date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
    

提交回复
热议问题