计算当前时间相对于日出和日落时间的百分比?
Calculate current time in percentage in relation to sunrise and sunset time?
我认为我想要实现的目标最好用我在下面制作的图表来描述:
我正在从 API 中获取日出和日落数据,returns 对象如下:
{
sunrise: "07:58:38",
sunset: "15:52:46",
day_length: "07:54:08",
}
我目前正在使用这个 if 语句计算当前是白天还是晚上:
const date = moment().format("YYYY-MM-DD")
let dayOrNight = "Day"
if (moment().isAfter(`${date} ${location.sunrise}`) && moment().isBefore(`${date} ${location.sunset}`)) {
dayOrNight = "Day"
} else {
dayOrNight = "Night"
}
我只需要一个基于当前时间相对于此日出和日落数据的百分比值。
例如,如果这是我的数据集:
{
sunrise: "00:00:00",
sunset: "10:00:00",
}
并且我的当前时间完全介于日出和日落时间之间,在这种情况下为 05:00:00
或 17:00:00
,那么我的当前时间将等于 100%。 02:30:00
、07:30:00
等同于 50%。
嗨,
解决方案
我认为使用秒是合乎逻辑的。
- 找出白天的中点和夜晚的中点时间。 (day_midpoint_in_sec + 12 x 60 x 60 秒将是夜间中点)
- 找到 percent_per_second 白天和黑夜(白天 =>
(day_midpoint_in_sec - sunrise_in_sec) / 100
)
- 查找 given_time 是否为 day/night。 (你已经解决了)
- 如果是白天则设置
elapsed_sec = given_time_in_sec - sunrise_in_sec
- 如果 given_time > day_midpoint,百分比为
200 - (elapsed_sec x percent_per_second)
- 否则百分比是
elapsed_sec x percent_per_second
同样,您可以轻松地进行夜间锻炼。
或
再三思考,并检查图表,
- 找到与日出有关的一切。 (day_midpoint_in_sec,night_midpoint_in_sec 和日落)
- 根据 given_time_in_sec 所在的位置(好旧的
y = mx + c
)应用方程式以获得百分比
我们将得到4个方程式:-
y = (100x)/day_midpoint_in_sec
y = (-100x/(sunset_in_sec - day_midpoint_in_sec)) + 100
y = 100x/(night_midpoint_in_sec - sunset)
y = (-100x/(24*60*60 - sunset_in_sec) - sunset_in_sec) + 100
编辑
嗨,由于第二个解决方案变得更加复杂(我也注意到一些边缘情况和我的错误),我已经根据第一个解决方案制作了一个工作片段。
let api = {
sunrise: "00:00:00",
sunset: "10:00:00",
};
const date = moment().format("YYYY-MM-DD")
let sunrise = moment(`${date} ${api.sunrise}`);
let sunset = moment(`${date} ${api.sunset}`);
let hour_24 = moment(`${date} 24:00:00`);
let hour_0 = moment(`${date} 00:00:00`);
let sunset_till_hour24 = hour_24.clone().subtract(sunset);
let hour24_till_sunrise = sunrise.clone().subtract(hour_0);
let midnight = sunset.clone().add(sunset_till_hour24.clone().add(hour24_till_sunrise).valueOf()/2);
let midday = sunrise.clone().add(sunset.clone().subtract(sunrise).valueOf()/2);
let givenTime = "17:10:00";
let given_time = moment(`${date} ${givenTime}`);
function findTimePercent(time, sunrise, midday, sunset, midnight) {
let day_percent_per_ms = 100/midday.clone().subtract(sunrise).valueOf();
let night_percent_per_ms = 100/midnight.clone().subtract(sunset).valueOf();
let percent = 0;
if (time.isSame(sunrise) || time.isSame(sunset)) {
return 0;
} else if (time.isSame(midday) || time.isSame(midnight)) {
return 100;
}
// isBetween is exclusive
if (time.isBetween(sunrise, midday)) {
percent = time.subtract(sunrise).valueOf() * day_percent_per_ms;
} else if(time.isBetween(midday, sunset)) {
percent = 100 - (time.subtract(midday).valueOf() * day_percent_per_ms);
} else if(time.isBetween(sunset, midnight)) {
percent = time.subtract(sunset).valueOf() * night_percent_per_ms;
} else {
percent = 100 - (time.subtract(midnight).valueOf() * night_percent_per_ms);
}
return percent;
}
console.log(findTimePercent(given_time, sunrise, midday, sunset, midnight));
我希望这是不言自明的。
我认为我想要实现的目标最好用我在下面制作的图表来描述:
我正在从 API 中获取日出和日落数据,returns 对象如下:
{
sunrise: "07:58:38",
sunset: "15:52:46",
day_length: "07:54:08",
}
我目前正在使用这个 if 语句计算当前是白天还是晚上:
const date = moment().format("YYYY-MM-DD")
let dayOrNight = "Day"
if (moment().isAfter(`${date} ${location.sunrise}`) && moment().isBefore(`${date} ${location.sunset}`)) {
dayOrNight = "Day"
} else {
dayOrNight = "Night"
}
我只需要一个基于当前时间相对于此日出和日落数据的百分比值。
例如,如果这是我的数据集:
{
sunrise: "00:00:00",
sunset: "10:00:00",
}
并且我的当前时间完全介于日出和日落时间之间,在这种情况下为 05:00:00
或 17:00:00
,那么我的当前时间将等于 100%。 02:30:00
、07:30:00
等同于 50%。
嗨,
解决方案
我认为使用秒是合乎逻辑的。
- 找出白天的中点和夜晚的中点时间。 (day_midpoint_in_sec + 12 x 60 x 60 秒将是夜间中点)
- 找到 percent_per_second 白天和黑夜(白天 =>
(day_midpoint_in_sec - sunrise_in_sec) / 100
) - 查找 given_time 是否为 day/night。 (你已经解决了)
- 如果是白天则设置
elapsed_sec = given_time_in_sec - sunrise_in_sec
- 如果 given_time > day_midpoint,百分比为
200 - (elapsed_sec x percent_per_second)
- 否则百分比是
elapsed_sec x percent_per_second
同样,您可以轻松地进行夜间锻炼。
或
再三思考,并检查图表,
- 找到与日出有关的一切。 (day_midpoint_in_sec,night_midpoint_in_sec 和日落)
- 根据 given_time_in_sec 所在的位置(好旧的
y = mx + c
)应用方程式以获得百分比
我们将得到4个方程式:-
y = (100x)/day_midpoint_in_sec
y = (-100x/(sunset_in_sec - day_midpoint_in_sec)) + 100
y = 100x/(night_midpoint_in_sec - sunset)
y = (-100x/(24*60*60 - sunset_in_sec) - sunset_in_sec) + 100
编辑
嗨,由于第二个解决方案变得更加复杂(我也注意到一些边缘情况和我的错误),我已经根据第一个解决方案制作了一个工作片段。
let api = {
sunrise: "00:00:00",
sunset: "10:00:00",
};
const date = moment().format("YYYY-MM-DD")
let sunrise = moment(`${date} ${api.sunrise}`);
let sunset = moment(`${date} ${api.sunset}`);
let hour_24 = moment(`${date} 24:00:00`);
let hour_0 = moment(`${date} 00:00:00`);
let sunset_till_hour24 = hour_24.clone().subtract(sunset);
let hour24_till_sunrise = sunrise.clone().subtract(hour_0);
let midnight = sunset.clone().add(sunset_till_hour24.clone().add(hour24_till_sunrise).valueOf()/2);
let midday = sunrise.clone().add(sunset.clone().subtract(sunrise).valueOf()/2);
let givenTime = "17:10:00";
let given_time = moment(`${date} ${givenTime}`);
function findTimePercent(time, sunrise, midday, sunset, midnight) {
let day_percent_per_ms = 100/midday.clone().subtract(sunrise).valueOf();
let night_percent_per_ms = 100/midnight.clone().subtract(sunset).valueOf();
let percent = 0;
if (time.isSame(sunrise) || time.isSame(sunset)) {
return 0;
} else if (time.isSame(midday) || time.isSame(midnight)) {
return 100;
}
// isBetween is exclusive
if (time.isBetween(sunrise, midday)) {
percent = time.subtract(sunrise).valueOf() * day_percent_per_ms;
} else if(time.isBetween(midday, sunset)) {
percent = 100 - (time.subtract(midday).valueOf() * day_percent_per_ms);
} else if(time.isBetween(sunset, midnight)) {
percent = time.subtract(sunset).valueOf() * night_percent_per_ms;
} else {
percent = 100 - (time.subtract(midnight).valueOf() * night_percent_per_ms);
}
return percent;
}
console.log(findTimePercent(given_time, sunrise, midday, sunset, midnight));
我希望这是不言自明的。