Fix 7-day forecast day labels shifted by UTC date parsing
This commit is contained in:
parent
adeb1690e8
commit
bc7780d494
@ -1,6 +1,7 @@
|
||||
<script>
|
||||
import { app } from '../lib/stores/app.svelte.js'
|
||||
import { getWeatherInfo } from '../lib/api/weather-codes.js'
|
||||
import { parseLocalDate } from '../lib/dates.js'
|
||||
|
||||
const data = $derived(app.forecastData)
|
||||
const daily = $derived(data?.daily)
|
||||
@ -10,14 +11,14 @@
|
||||
function formatDay(dateStr, index) {
|
||||
if (index === 0) return 'Today'
|
||||
if (index === 1) return 'Tomorrow'
|
||||
const d = new Date(dateStr)
|
||||
const d = parseLocalDate(dateStr)
|
||||
return d.toLocaleDateString('en-US', { weekday: 'short', month: 'short', day: 'numeric' })
|
||||
}
|
||||
|
||||
function getDayName(dateStr, index) {
|
||||
if (index === 0) return 'Today'
|
||||
if (index === 1) return 'Tomorrow'
|
||||
return new Date(dateStr).toLocaleDateString('en-US', { weekday: 'long' })
|
||||
return parseLocalDate(dateStr).toLocaleDateString('en-US', { weekday: 'long' })
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
13
src/lib/dates.js
Normal file
13
src/lib/dates.js
Normal file
@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Date helpers for Open-Meteo strings.
|
||||
*
|
||||
* Open-Meteo returns calendar dates like "2026-08-19" (in the location's
|
||||
* timezone). `new Date("2026-08-19")` parses that as UTC midnight, so in U.S.
|
||||
* Central (UTC-5) it renders as the PREVIOUS weekday, shifting every day
|
||||
* label one day back. Build the Date from its parts instead so it is a local
|
||||
* calendar date with no offset/timezone shift.
|
||||
*/
|
||||
export function parseLocalDate(dateStr) {
|
||||
const [y, m, d] = String(dateStr).split('-').map(Number)
|
||||
return new Date(y, (m || 1) - 1, d || 1)
|
||||
}
|
||||
@ -7,6 +7,7 @@
|
||||
import { isSevereWeather } from '../api/weather-codes.js'
|
||||
import { app } from './app.svelte.js'
|
||||
import { saveSettings } from '../storage/db.js'
|
||||
import { parseLocalDate } from '../dates.js'
|
||||
|
||||
export class NotificationStore {
|
||||
/** @type {Array<{ id: string, type: string, severity: 'info'|'warning'|'danger', message: string, icon: string }>} */
|
||||
@ -66,7 +67,7 @@ export class NotificationStore {
|
||||
if (daily.time) {
|
||||
for (let i = 0; i < daily.time.length && i < 3; i++) {
|
||||
const day = daily.time[i]
|
||||
const dateLabel = i === 0 ? 'Today' : i === 1 ? 'Tomorrow' : new Date(day).toLocaleDateString('en-US', { weekday: 'short' })
|
||||
const dateLabel = i === 0 ? 'Today' : i === 1 ? 'Tomorrow' : parseLocalDate(day).toLocaleDateString('en-US', { weekday: 'short' })
|
||||
|
||||
const precip = daily.precipitation_probability_max?.[i] || 0
|
||||
if (precip >= t.precip) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user