Skip to content
This repository has been archived by the owner on Aug 28, 2022. It is now read-only.

Commit

Permalink
fix: wrong sunset and sunrise time on Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
pjchender committed Mar 2, 2020
1 parent 5107b07 commit e37e940
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/WeatherApp.js
@@ -1,6 +1,7 @@
import React, { useState, useEffect, useMemo } from 'react';
import styled from '@emotion/styled';
import { ThemeProvider } from 'emotion-theming';
import dayjs from 'dayjs';
import WeatherCard from './WeatherCard';
import useWeatherApi from './useWeatherApi';
import sunriseAndSunsetData from './sunrise-sunset.json';
Expand Down Expand Up @@ -42,7 +43,7 @@ const getMoment = (locationName) => {

if (!location) return null;

const now = new Date();
const now = dayjs();
const nowDate = Intl.DateTimeFormat('zh-TW', {
year: 'numeric',
month: '2-digit',
Expand All @@ -53,13 +54,14 @@ const getMoment = (locationName) => {

const locationDate =
location.time && location.time.find((time) => time.dataTime === nowDate);
const sunriseTimestamp = new Date(
const sunriseTimestamp = dayjs(
`${locationDate.dataTime} ${locationDate.sunrise}`
).getTime();
const sunsetTimestamp = new Date(
).unix();
const sunsetTimestamp = dayjs(
`${locationDate.dataTime} ${locationDate.sunset}`
).getTime();
const nowTimeStamp = now.getTime();
).unix();

const nowTimeStamp = now.unix();

return sunriseTimestamp <= nowTimeStamp && nowTimeStamp <= sunsetTimestamp
? 'day'
Expand Down

0 comments on commit e37e940

Please sign in to comment.