Skip to content

Commit

Permalink
added getDateString
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardomcv committed Jul 21, 2020
1 parent e304b3d commit 8cc897a
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/components/DataSection/DataSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import { OPEN_WEATHER_ICONS_ENDPOINT } from '../../config';

const ICON_EXTENSION = '@2x.png';

function getDateString(unixTimestamp: number) {
// convert UNIX timestamp to miliseconds
const timestamp = unixTimestamp * 1000;

// return localized time string
return new Date(timestamp).toLocaleTimeString();
}

interface DataSectionProps {
selectedLocation: Location;
selectedTemperatureUnit: TemperatureUnit;
Expand Down Expand Up @@ -47,9 +55,8 @@ const DataSection: FunctionComponent<DataSectionProps> = ({
const tempSymbol = selectedTemperatureUnit === 'celsius' ? '°C' : '°F';
const weatherIcon = weather[0].icon; // first element is the "primary"

// convert to UNIX timestamp with miliseconds
const sunriseDate = new Date(sys.sunrise * 1000).toLocaleTimeString();
const sunsetDate = new Date(sys.sunset * 1000).toLocaleTimeString();
const sunriseDateString = getDateString(sys.sunrise);
const sunsetDateString = getDateString(sys.sunset);

return (
<section className="data-section">
Expand All @@ -62,8 +69,8 @@ const DataSection: FunctionComponent<DataSectionProps> = ({
/>
</div>
<div className="day-cycle">
<span>{`Sunrise: ${sunriseDate}`}</span>
<span>{`Sunset: ${sunsetDate}`}</span>
<span>{`Sunrise: ${sunriseDateString}`}</span>
<span>{`Sunset: ${sunsetDateString}`}</span>
</div>
</section>
);
Expand Down

0 comments on commit 8cc897a

Please sign in to comment.