首页 > 解决方案 > 用于一系列日期的日期字符串

问题描述

我在使用此功能时遇到问题,基本上我从两个不同的日历中选择两个日期,在文本框中打印这些日期,然后我想获得对存储在 JSON 中的一些信息的访问权限(就像在注释行中所做的那样)分别地。我试图从 selectedDate_start 和 selectedDate_end 之间的每一天获取每个 CountyInfectionNumber 和每个发病率。

如您所见,格式有点不稳定,但这就是它在我的 JSON 文件中的编写方式。

我的问题在于这两行:

DateTime loadedDate_start = DateTime.ParseExact(chosenDate_start, "d", null);
DateTime loadedDate_end = DateTime.ParseExact(chosenDate_end, "d", null);

我究竟做错了什么?它说 selectedDate_ 不能是字符串,但这不是 ParseExact 应该做的吗?

@insane_developer 在评论中解决了这个问题,支持他

        {
            using (var webClient = new System.Net.WebClient())
            {
                json = webClient.DownloadString("https://datelazi.ro/latestData.json");
            }
            var semiParsedData = JObject.Parse(json);

            string chosenDate_start;
            string chosenDate_end;
            string chosenCounty;

            ZiAleasaatunci.Text = Calendar_de_Ales_Inc.SelectionRange.Start.ToString("yyyy-MM-dd");
            ZiSfarsitAleasaAtunci.Text = Calendar_Ales_Sf.SelectionRange.End.ToString("yyyy-MM-dd");

            chosenDate_start = ZiAleasaatunci.Text;
            chosenDate_end = ZiSfarsitAleasaAtunci.Text;
            chosenCounty = JudeteAtunci.Text;

            
            //var countySpecificInfections = semiParsedData.SelectToken($"historicalData.{chosenDate_start}.countyInfectionsNumbers.{chosenCounty}");
            //var countySpecificIncidence = semiParsedData.SelectToken($"historicalData.{chosenDate_start}.incidence.{chosenCounty}");
           
            //NumarCazuriAtunci.Text = countySpecificInfections.Value<int>().ToString();
            //IncidentaAtunci.Text = countySpecificIncidence.Value<double>().ToString();
            

            DateTime loadedDate_start = DateTime.ParseExact(chosenDate_start, "d", null);
            DateTime loadedDate_end = DateTime.ParseExact(chosenDate_end, "d", null);

            List<DateTime> allDates = new List<DateTime>();

            

            DateTime Zi_Aleasa = loadedDate_start ;
            DateTime Zi_Precedenta = Zi_Aleasa.AddDays(-1);
            Console.WriteLine(Zi_Precedenta);

             
            for (DateTime date = loadedDate_start; date <= loadedDate_end; date = date.AddDays(1))
                allDates.Add(date);
  
           


        } 

标签: c#

解决方案


推荐阅读