@@ -449,10 +449,20 @@ function updateTodayForecast() {
449449 const now = getLocationTime ( Math . floor ( Date . now ( ) / 1000 ) ) ;
450450 const currentHour = now . getHours ( ) ;
451451 now . setMinutes ( 0 , 0 , 0 ) ;
452- const todayForecasts = forecastData . list . filter ( forecast => {
452+ let todayForecasts = forecastData . list . filter ( forecast => {
453453 const forecastDate = getLocationTime ( forecast . dt ) ;
454454 return forecastDate . toDateString ( ) === now . toDateString ( ) ;
455455 } ) ;
456+ if ( todayForecasts . length === 0 ) {
457+ const sortedForecasts = [ ...forecastData . list ] . sort ( ( a , b ) => b . dt - a . dt ) ;
458+ const lastForecast = sortedForecasts . find ( forecast => {
459+ const forecastDate = getLocationTime ( forecast . dt ) ;
460+ return forecastDate <= now ;
461+ } ) ;
462+ if ( lastForecast ) {
463+ todayForecasts = [ lastForecast ] ;
464+ }
465+ }
456466 todayForecasts . forEach ( forecast => {
457467 const hourElement = document . createElement ( "div" ) ;
458468 const forecastTime = getLocationTime ( forecast . dt ) ;
@@ -507,14 +517,24 @@ function updateForecast() {
507517 const tempUnit = currentUnit === "metric" ? "°C" : "°F" ;
508518 if ( currentDisplayMode === "all" ) {
509519 forecastDates . forEach ( ( date ) => {
510- const dayData = dailyForecasts [ date ] ;
511- createDayForecastElement ( date , dayData . forecasts , dayData . minTemp , dayData . maxTemp , false , tempUnit ) ;
520+ createDayForecastElement (
521+ date ,
522+ dailyForecasts [ date ] . forecasts ,
523+ dailyForecasts [ date ] . minTemp ,
524+ dailyForecasts [ date ] . maxTemp ,
525+ false ,
526+ tempUnit
527+ ) ;
512528 } ) ;
513- } else {
529+ } else if ( currentDisplayMode === "day" && forecastDates [ currentSelectedDay ] ) {
514530 const selectedDate = forecastDates [ currentSelectedDay ] ;
515- if ( selectedDate && dailyForecasts [ selectedDate ] ) {
516- const dayData = dailyForecasts [ selectedDate ] ;
517- createDayForecastElement ( selectedDate , dayData . forecasts , dayData . minTemp , dayData . maxTemp , false , tempUnit ) ;
518- }
531+ createDayForecastElement (
532+ selectedDate ,
533+ dailyForecasts [ selectedDate ] . forecasts ,
534+ dailyForecasts [ selectedDate ] . minTemp ,
535+ dailyForecasts [ selectedDate ] . maxTemp ,
536+ false ,
537+ tempUnit
538+ ) ;
519539 }
520540}
0 commit comments