简体   繁体   中英

Failed to Convert UTC TIME string date to Epoch time in C#

I read a azure health check from azure using rest api. Following is my response

    {
           "name": "current",          
            "reasonType": "",
            "occuredTime": "2020-07-31T11:02:22Z",
            "reasonChronicity": "Persistent",
            "reportedTime": "2020-08-22T09:36:18.1858299Z"
        
    }

I want to convert tthe reported time to epoch time format. my code is

var azobject = saas.ToObject<JObject>();
var ty =  azobject["reportedTime"];
var ssa = DateTime.Parse(ty.ToString());

var dateTimeOffset = new DateTimeOffset(ssa);
var unixDateTime = dateTimeOffset.ToUnixTimeSeconds();

Value is totally different and it always shows the local time.After processing this code,the value of azobject["reportedTime"] format is totally different than what i get from response({22/08/2020 11:31:43 AM} theres is T and z is not present).

This is basically because the .net json package rewrites the method of type conversion. If you want to get the same result as the original, please do this:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "https://management.azure.com//subscriptions/xxxxxx?api-version=xxxx-xx-xx-xx";
            string tokenbear = "eyJ0exxxxxxPw";
            WebRequest request = HttpWebRequest.Create(url);
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization =
                  new AuthenticationHeaderValue("Bearer", tokenbear);
                var test = client.GetAsync(url);
                var b = test.Result.Content.ReadAsStringAsync().Result;
                JObject json = JObject.Parse(b);
                string test3 = json["properties"].ToString();
                Console.WriteLine(test3 + "\n");
                var settings = new JsonSerializerSettings
                {
                    DateFormatString = "yyyy-MM-ddTH:mm:ss.fffffffZ",
                    DateTimeZoneHandling = DateTimeZoneHandling.Utc
                };
                var test2 = json["properties"]["reportedTime"];
                var _test2 = JsonConvert.SerializeObject(test2, settings);
                _test2 = _test2.Replace("\"", "");
                Console.WriteLine(_test2.ToString());
            }
        }
    }
}

Then you will get the same value as the value in your response:

在此处输入图像描述

After obtaining the original data, you can perform the processing you want. On my side it is no problem, please have a try on your side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM