Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/common/datetime/format_date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ export const formatDate = toLocaleDateStringSupportsOptions
day: "numeric",
})
: (dateObj: Date) => format(dateObj, "longDate");

export const formatDateWeekday = toLocaleDateStringSupportsOptions
? (dateObj: Date, locales: string) =>
dateObj.toLocaleDateString(locales, {
weekday: "long",
month: "short",
day: "numeric",
})
: (dateObj: Date) => format(dateObj, "dddd, D MMM");
Comment thread
spacegaier marked this conversation as resolved.
Outdated
9 changes: 9 additions & 0 deletions src/common/datetime/format_time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ export const formatTimeWithSeconds = toLocaleTimeStringSupportsOptions
second: "2-digit",
})
: (dateObj: Date) => format(dateObj, "mediumTime");

export const formatTimeWeekday = toLocaleTimeStringSupportsOptions
? (dateObj: Date, locales: string) =>
dateObj.toLocaleTimeString(locales, {
weekday: "long",
hour: "numeric",
minute: "2-digit",
})
: (dateObj: Date) => format(dateObj, "dddd, HH:mm");
29 changes: 10 additions & 19 deletions src/dialogs/more-info/controls/more-info-weather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
PropertyValues,
} from "lit-element";
import { html, TemplateResult } from "lit-html";
import { formatDateWeekday } from "../../../common/datetime/format_date";
import { formatTimeWeekday } from "../../../common/datetime/format_time";
import { formatNumber } from "../../../common/string/format_number";
import "../../../components/ha-svg-icon";
import { getWeatherUnit, getWind } from "../../../data/weather";
Expand Down Expand Up @@ -180,14 +182,20 @@ class MoreInfoWeather extends LitElement {
${!this._showValue(item.templow)
? html`
<div class="main">
${this.computeDateTime(item.datetime)}
${formatTimeWeekday(
new Date(item.datetime),
this.hass.language
)}
</div>
`
: ""}
${this._showValue(item.templow)
? html`
<div class="main">
${this.computeDate(item.datetime)}
${formatDateWeekday(
new Date(item.datetime),
this.hass.language
)}
</div>
<div class="templow">
${formatNumber(item.templow, this.hass!.language)}
Expand Down Expand Up @@ -253,23 +261,6 @@ class MoreInfoWeather extends LitElement {
`;
}

private computeDate(data) {
const date = new Date(data);
return date.toLocaleDateString(this.hass.language, {
weekday: "long",
month: "short",
day: "numeric",
});
}

private computeDateTime(data) {
const date = new Date(data);
return date.toLocaleDateString(this.hass.language, {
weekday: "long",
hour: "numeric",
});
}

private _showValue(item: string): boolean {
return typeof item !== "undefined" && item !== null;
}
Expand Down
9 changes: 4 additions & 5 deletions src/panels/lovelace/cards/hui-weather-forecast-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PropertyValues,
TemplateResult,
} from "lit-element";
import { formatTime } from "../../../common/datetime/format_time";
import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element";
import { fireEvent } from "../../../common/dom/fire_event";
import { computeStateDisplay } from "../../../common/entity/compute_state_display";
Expand Down Expand Up @@ -284,11 +285,9 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
`
: hourly
? html`
${new Date(item.datetime).toLocaleTimeString(
this.hass!.language,
{
hour: "numeric",
}
${formatTime(
new Date(item.datetime),
this.hass!.language
)}
`
: html`
Expand Down