如果日期少于或超过15分钟(如果日落或日出)
If date less or more than 15 minuites (If Sunset or Sunrise)
我正在尝试使用日期,现在是日落或日出。
因为我不知道如何处理If Else语句关于日期
我得到了日落和日出的数据。
但我想知道如果当前日期是日落或日出,如何使用+-15 minutes
进行条件设置
示例:
当前日期:2015-12-17T17:45:00(或17:45-18:15之间)
日落时间:2015-12-17T18:00:00
Rusult=日落(因为+-15 minutes
)
这是我迄今为止的代码:
var SunData = {'set':'2015-12-17T09:14:56', 'rise':'2015-12-17T15:10:52'};
var date = new Date();
date = date.getTime();
var sun_rise = new Date(SunData['rise']);
sun_rise = sun_rise.getTime();
var sun_set = new Date(SunData['set']);
sun_set = sun_set.getTime();
var sunRiseSetDiff = 900000; // 15 minutes
// Here
var result;
if(false) {
result = 'sun-rise';
} else if (false) {
result = 'sun-set';
} else {
result = 'none';
}
https://jsfiddle.net/gd1s1o0y/
关于If Else语句,你能帮我吗?感谢
你是说吗
var result = 'none';
if(Math.abs(date - sun_rise) <= sunRiseSetDiff) {
result = 'sun-rise';
}
else if(Math.abs(date - sun_set) <= sunRiseSetDiff) {
result = 'sun-set';
}
Fiddle
检查这个小提琴
var SunData = {'set':'2015-12-17T09:14:56', 'rise':'2015-12-17T15:10:52'};
var date = new Date();
date = date.getTime();
var sun_rise = new Date(SunData['rise']);
sun_rise = sun_rise.getTime();
var sun_set = new Date(SunData['set']);
sun_set = sun_set.getTime();
var sunRiseSetDiff = 900000; // 15 minutes
// Here
var sunsetResult = Math.abs( date - sun_set ) > sunRiseSetDiff ? false: true;
var sunriseResult = Math.abs( date - sun_rise ) > sunRiseSetDiff ? false: true;
if(sunriseResult) {
result = 'sun-rise';
} else if (sunsetResult) {
result = 'sun-rise';
} else {
result = 'none';
}
alert(result);
只是用注释重写函数。
// Always parse date strings manually, you can use a library if you want
// but a bespoke function is pretty simple if the format is guaranteed
// and is a valid date
function qParse(s) {
var b = s.split(/'D/);
return new Date(b[0], b[1]-1, b[2], b[3]||0, b[4]||0, b[5]||0, b[6]||0);
}
// The OP seemed to have sunrise and sunset transposed
var sunData = {'rise':'2015-12-17T09:14:56', 'set':'2015-12-17T15:10:52'};
// There is no need for getTime, date objects can be used in simple
// arithmetic directly
var now = new Date();
var buffer = 9e5; // 15 mintues
// Initialise result to a default string
var result = 'Not near sunrise or sunset';
// Change value if near sunset
if (Math.abs(now - qParse(sunData.set)) <= buffer) {
result = "Close to sunset";
// Otherwise, change value if near sunrise
} else if (Math.abs(now - qParse(sunData.rise)) <= buffer) {
result = "Close to sunrise";
}
// Show result
document.write(result);
相关文章:
- 如果 a 为 false,则 if(a) === if(false)
- RegEx删除空属性?例如,如果(class=“”||class=“”)移除;否则就下课
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 如果文本字段为空,则使用JavaScript应用CSS样式
- 如果我在javascript中输入无效的电子邮件或空白,如何显示特定的文本框边框红色
- 如果href包含X,请更改href
- 如果localstorage为空,则显示欢迎消息
- 点击后隐藏潜水?(但如果Div是一面旗帜呢?)
- 如果元素's的ID以数字开头
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- Jquery模板,如果xx&&如果yy
- 如果值为空,如何设置输入的默认值?jQuery
- 如果选中了多个复选框,如何添加事件
- 如何检查元素的内容是否为空,如果为空,请在jquery中删除该元素
- 如何知道元素在屏幕上是否可见.如果没有,请滚动页面
- Javascript,如果条件在没有&&逻辑运算符当&&它不起作用
- 一个密码测试程序,如果存在空格,它会提醒用户
- 如果使用javascript函数屏幕太小,我该如何更改HTML文件的背景色
- 如果日期少于或超过15分钟(如果日落或日出)