Pick up time from the nearest array to the current time

Asked

Viewed 43 times

0

I have the following Json:

{
"cod": "200",
"message": 0.0055,
"cnt": 40,
"list": [
    {
        "dt": 1560319200,
        "main": {
            "temp": 15.15,
            "temp_min": 14.49,
            "temp_max": 15.15,
            "pressure": 1021.11,
            "sea_level": 1021.11,
            "grnd_level": 995.76,
            "humidity": 96,
            "temp_kf": 0.66
        },
        "weather": [
            {
                "id": 801,
                "main": "Clouds",
                "description": "céu pouco nublado",
                "icon": "02n"
            }
        ],
        "clouds": {
            "all": 19
        },
        "wind": {
            "speed": 0.78,
            "deg": 246.743
        },
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2019-06-12 06:00:00"
    },
    {
        "dt": 1560330000,
        "main": {
            "temp": 14.69,
            "temp_min": 14.19,
            "temp_max": 14.69,
            "pressure": 1020.69,
            "sea_level": 1020.69,
            "grnd_level": 995.4,
            "humidity": 95,
            "temp_kf": 0.5
        },
        "weather": [
            {
                "id": 801,
                "main": "Clouds",
                "description": "céu pouco nublado",
                "icon": "02n"
            }
        ],
        "clouds": {
            "all": 17
        },
        "wind": {
            "speed": 0.7,
            "deg": 256.122
        },
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2019-06-12 09:00:00"
    },
    {
        "dt": 1560340800,
        "main": {
            "temp": 19.68,
            "temp_min": 19.35,
            "temp_max": 19.68,
            "pressure": 1021.93,
            "sea_level": 1021.93,
            "grnd_level": 996.86,
            "humidity": 74,
            "temp_kf": 0.33
        },
        "weather": [
            {
                "id": 801,
                "main": "Clouds",
                "description": "céu pouco nublado",
                "icon": "02d"
            }
        ],
        "clouds": {
            "all": 23
        },
        "wind": {
            "speed": 0.26,
            "deg": 210.741
        },
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2019-06-12 12:00:00"
    },
    {
        "dt": 1560351600,
        "main": {
            "temp": 25.72,
            "temp_min": 25.55,
            "temp_max": 25.72,
            "pressure": 1020.78,
            "sea_level": 1020.78,
            "grnd_level": 995.84,
            "humidity": 55,
            "temp_kf": 0.17
        },
        "weather": [
            {
                "id": 802,
                "main": "Clouds",
                "description": "nuvens dispersas",
                "icon": "03d"
            }
        ],
        "clouds": {
            "all": 36
        },
        "wind": {
            "speed": 1.34,
            "deg": 72.064
        },
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2019-06-12 15:00:00"
    },
    {
        "dt": 1560362400,
        "main": {
            "temp": 25.68,
            "temp_min": 25.68,
            "temp_max": 25.68,
            "pressure": 1018.44,
            "sea_level": 1018.44,
            "grnd_level": 993.46,
            "humidity": 59,
            "temp_kf": 0
        },
        "weather": [
            {
                "id": 802,
                "main": "Clouds",
                "description": "nuvens dispersas",
                "icon": "03d"
            }
        ],
        "clouds": {
            "all": 47
        },
        "wind": {
            "speed": 2.12,
            "deg": 81.239
        },
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2019-06-12 18:00:00"
    },
    {
        "dt": 1560373200,
        "main": {
            "temp": 18.45,
            "temp_min": 18.45,
            "temp_max": 18.45,
            "pressure": 1019.96,
            "sea_level": 1019.96,
            "grnd_level": 994.89,
            "humidity": 97,
            "temp_kf": 0
        },
        "weather": [
            {
                "id": 802,
                "main": "Clouds",
                "description": "nuvens dispersas",
                "icon": "03n"
            }
        ],
        "clouds": {
            "all": 25
        },
        "wind": {
            "speed": 0.93,
            "deg": 76.605
        },
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2019-06-12 21:00:00"
    },
    {
        "dt": 1560384000,
        "main": {
            "temp": 17.25,
            "temp_min": 17.25,
            "temp_max": 17.25,
            "pressure": 1021.66,
            "sea_level": 1021.66,
            "grnd_level": 996.24,
            "humidity": 97,
            "temp_kf": 0
        },
        "weather": [
            {
                "id": 803,
                "main": "Clouds",
                "description": "nuvens quebradas",
                "icon": "04n"
            }
        ],
        "clouds": {
            "all": 59
        },
        "wind": {
            "speed": 0.68,
            "deg": 309.784
        },
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2019-06-13 00:00:00"
    }
],
"city": {
    "id": 3459712,
    "name": "Joinville",
    "coord": {
        "lat": -26.3045,
        "lon": -48.8487
    },
    "country": "BR",
    "population": 461304,
    "timezone": -10800
  }
}

I would like to know how I can get the information of the array that is closest to the current time, and the array brings the information every 3 hours.

Ex: "dt_txt": "2019-06-12 12:00:00", is closer to 12:42(now) than 15:00 hours:

I already own the organized object:

var result = weather.list.Select(x => new { day = x.dt_txt,
                        tempMax = x.main.MaximumTemperature,
                        tempMin = x.main.MinimumTemperature,
                        Hum = x.main.Humidity}).OrderBy(x => x.day);

1 answer

1


If you convert the string to Datetime, you can calculate the difference between the record date and the current one using Math.Abs() in the OrderBy() and catch the first result.

var result = weather.list.Select(x => new { day = DateTime.Parse(x.dt_txt),
                                            tempMax = x.main.MaximumTemperature,
                                            tempMin = x.main.MinimumTemperature,
                                            Hum = x.main.Humidity
                                 })
                                 .OrderBy(x => Math.Abs(x.day.Ticks - DateTime.Now.Ticks))
                                 .FirstOrDefault();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.