简体   繁体   中英

How to compare current date with iteration for 5 days ahead and date from object which has a value every three hours

I want for each date with a weather forecast that has data every three hours to take the largest minimum and average value.

I manage to write in a list the values for today but then the dates in the check diverge and the debugger skips the check, therefore the same values are written for the other days but they are not correct.

I write this code:

var tempArray = weatherBindingData.WeatherDataForecastHourly.List;

            var TemperaturesList = new List<TemperatureMinMaxAvg>();

            DateTime CurrentDate = DateTime.Now;

            for (var i = 0; i < 5; i++)
            {
                double TempMinValue = 100;

                double TempMaxValue = -100;

                var DateTimeNow = CurrentDate.ToString("yyyy-MM-dd");

                var DateTimeNowDateOnly = CurrentDate.Date;

                foreach (var item in tempArray)
                {
                    DateTime DatesTemp1 = DateTime.ParseExact(item.DateTimeFull, "yyyy-MM-dd HH:mm:ss",
                                               System.Globalization.CultureInfo.InvariantCulture);

                    var TempDayNowDateOnly = DatesTemp1.Date;

                    var TempDay = DatesTemp1.ToString("yyyy-MM-dd");

                    if (TempDay == DateTimeNow)
                    {
                        TempMinValue = Math.Min(TempMinValue, item.MainForecasts.TempMinForecast);

                        TempMaxValue = Math.Max(TempMaxValue, item.MainForecasts.TempMaxForecast);
                    }
                    TemperaturesList.Add(new TemperatureMinMaxAvg()
                    {
                        MinTemperature = TempMinValue,
                        MaxTemperature = TempMaxValue,
                        DateTimeFull = TempDay
                    });
                }
                
                CurrentDate.AddDays(1);
            }

For the current date, the indices are less because the data comes from the nearest hour from the API.

For example, if it is currently 17:00, the data for the current date will come in three indexes for 18:00 21:00 and 24:00. For tomorrow, the indices are 8 with data every three hours and so on for 5 days ahead. I want to take the smallest and largest value for today and for another 4 days ahead.

After the iteration that occurs the next day in each day after today the last data from the current day in the following days are recorded and the data becomes unreal.

My output data from TemperatureList is:

TemperaturesList    Count = 45  
[0] {WeatherLocationInfo.TemperatureMinMaxAvg}  
AvgTemperature  0   
DateTimeFull    "2021-04-06"    
MaxTemperature  9.58    
MinTemperature  9.17    
Non-public members      

[1] {WeatherLocationInfo.TemperatureMinMaxAvg}  
AvgTemperature  0   
DateTimeFull    "2021-04-06"    
MaxTemperature  12.2    
MinTemperature  9.17    
Non-public members      

[2] {WeatherLocationInfo.TemperatureMinMaxAvg}  
AvgTemperature  0   
DateTimeFull    "2021-04-06"    
MaxTemperature  13.53   
MinTemperature  9.17    
Non-public members  

[3] {WeatherLocationInfo.TemperatureMinMaxAvg}  
AvgTemperature  0   
DateTimeFull    "2021-04-07"    
MaxTemperature  13.53   
MinTemperature  9.17    
Non-public members      

Tha last values for today are recorded to other dates until the end.

This is input data:

weatherBindingData  {WeatherLocationInfo.WeatherBindingData}    
WeatherDataAirForecast  {WeatherLocationInfo.WeatherDataAir}    
WeatherDataCurrent  {WeatherLocationInfo.WeatherData}   
WeatherDataForecastHourly   {WeatherLocationInfo.WeatherDataForecast}   
List    {WeatherLocationInfo.List[40]}  

[0] {WeatherLocationInfo.List}  
ChanceOfRain    0   
CloudsForecast  {WeatherLocationInfo.CloudsForecast}    
DateTimeFull    "2021-04-06 15:00:00"   
MainForecasts   {WeatherLocationInfo.MainForecast}  
FeelsLikeForecast   8.62    
HumidityForecast    79  
PressureForecast    1017    
PressureGroundLevelForecast 1016    
SeaLevelForecast    1017    
TempForecast    9.16    
TempMaxForecast 9.58    
TempMinForecast 9.16    
Non-public members      
RainForecast    (null)  
SnowForecast    (null)  
WeatherForecast {WeatherLocationInfo.WeatherForecast[1]}    
WindForecast    {WeatherLocationInfo.WindForecast}  
Non-public members      

[1] {WeatherLocationInfo.List}  
ChanceOfRain    0   
CloudsForecast  {WeatherLocationInfo.CloudsForecast}    
DateTimeFull    "2021-04-06 18:00:00"   
MainForecasts   {WeatherLocationInfo.MainForecast}  
FeelsLikeForecast   10.48   
HumidityForecast    70  
PressureForecast    1018    
PressureGroundLevelForecast 1016    
SeaLevelForecast    1018    
TempForecast    11.45   
TempMaxForecast 12.2    
TempMinForecast 11.45   
Non-public members      
RainForecast    (null)  
SnowForecast    (null)  
WeatherForecast {WeatherLocationInfo.WeatherForecast[1]}    
WindForecast    {WeatherLocationInfo.WindForecast}  
Non-public members  

[2] {WeatherLocationInfo.List}  
ChanceOfRain    0   
CloudsForecast  {WeatherLocationInfo.CloudsForecast}    
DateTimeFull    "2021-04-06 21:00:00"   
MainForecasts   {WeatherLocationInfo.MainForecast}  
FeelsLikeForecast   12.27   
HumidityForecast    64  
PressureForecast    1017    
PressureGroundLevelForecast 1015    
SeaLevelForecast    1017    
TempForecast    13.22   
TempMaxForecast 13.53   
TempMinForecast 13.22   
Non-public members      
RainForecast    (null)  
SnowForecast    (null)  
WeatherForecast {WeatherLocationInfo.WeatherForecast[1]}    
WindForecast    {WeatherLocationInfo.WindForecast}  
Non-public members      

[3] {WeatherLocationInfo.List}  
ChanceOfRain    0   
CloudsForecast  {WeatherLocationInfo.CloudsForecast}    
DateTimeFull    "2021-04-07 00:00:00"   
MainForecasts   {WeatherLocationInfo.MainForecast}  
FeelsLikeForecast   11.68   
HumidityForecast    71  
PressureForecast    1018    
PressureGroundLevelForecast 1016    
SeaLevelForecast    1018    
TempForecast    12.52   
TempMaxForecast 12.55   
TempMinForecast 12.52   
Non-public members      
RainForecast    (null)  
SnowForecast    (null)  
WeatherForecast {WeatherLocationInfo.WeatherForecast[1]}    
WindForecast    {WeatherLocationInfo.WindForecast}  
Non-public members      

[4] {WeatherLocationInfo.List}  
ChanceOfRain    0   
CloudsForecast  {WeatherLocationInfo.CloudsForecast}    
DateTimeFull    "2021-04-07 03:00:00"   
MainForecasts   {WeatherLocationInfo.MainForecast}  
FeelsLikeForecast   9.64    
HumidityForecast    83  
PressureForecast    1019    
PressureGroundLevelForecast 1017    
SeaLevelForecast    1019    
TempForecast    10.38   
TempMaxForecast 10.38   
TempMinForecast 10.38   
Non-public members      
RainForecast    (null)  
SnowForecast    (null)  
WeatherForecast {WeatherLocationInfo.WeatherForecast[1]}    
WindForecast    {WeatherLocationInfo.WindForecast}  
Non-public members      

I think that the if statement itself does not work normally and after today's date is not performed and does not record correctly the data for the other 4 days.

If values are normalized to the same time zone, it appears you could compare just the dates and removed the hours:

var DateTimeNowDateOnly = CurrentDate.Date;

foreach (var item in tempArray)
{                
   var TempDayNowDateOnly = DatesTemp1.Date;
   if (TempDayNowDateOnly >= DateTimeNowDateOnly && 
       TempDayNowDateOnly < DateTimeNowDateOnly.AddDays(3) )
   {
    ...
   }
}

The output is the same and after today the debugger skips if statement.

    TemperaturesList    Count = 64  
[0] {WeatherLocationInfo.TemperatureMinMaxAvg}  
AvgTemperature  0   
DateTimeFull    "4/6/2021 12:00:00 AM"  
MaxTemperature  9.58    
MinTemperature  9.11    
Non-public members      

[1] {WeatherLocationInfo.TemperatureMinMaxAvg}  
AvgTemperature  0   
DateTimeFull    "4/6/2021 12:00:00 AM"  
MaxTemperature  12.2    
MinTemperature  9.11    
Non-public members      

[2] {WeatherLocationInfo.TemperatureMinMaxAvg}  
AvgTemperature  0   
DateTimeFull    "4/6/2021 12:00:00 AM"  
MaxTemperature  13.53   
MinTemperature  9.11    
Non-public members  

[3] {WeatherLocationInfo.TemperatureMinMaxAvg}  
AvgTemperature  0   
DateTimeFull    "4/7/2021 12:00:00 AM"  
MaxTemperature  13.53   
MinTemperature  9.11    
Non-public members      

[4] {WeatherLocationInfo.TemperatureMinMaxAvg}  
AvgTemperature  0   
DateTimeFull    "4/7/2021 12:00:00 AM"  
MaxTemperature  13.53   
MinTemperature  9.11    
Non-public members  

[5] {WeatherLocationInfo.TemperatureMinMaxAvg}  
AvgTemperature  0   
DateTimeFull    "4/7/2021 12:00:00 AM"  
MaxTemperature  13.53   
MinTemperature  9.11    
Non-public members      

The last value of today's date is recorded on all other dates until the end.

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