我必须得到上周五的时间戳,但我不知道如何使用工作日获得时间戳。有人可以帮忙吗?
我想要得到的是上周五和今天之间的区别。
var now = new Date();
var time = now.getTime();
var fridayTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 13, 30, 00);
var timeDiff = time - fridayTime;
我知道我没有为" fridayTime"编写正确的代码,但不确定正确的代码是什么。
答案 0 :(得分:8)
首先,您需要获得上周五的日期差异:
var now = new Date(),
day = now.getDay();
取决于"上周五"对你意味着,使用以下
最近的星期五
var diff = (day <= 5) ? (7 - 5 + day ) : (day - 5);
如果今天是星期五,那么今天返回,否则最接近星期五
var diff = (7 - 5 + day) % 7;
上周五
var diff = 7 - 5 + day;
然后从当前日期中减去此值,并使用setDate
函数将结果设置为日期对象。 setDate函数将正确处理负数,分别改变月份和年份:
来自mdn:
如果dayValue超出了该月的日期值范围, setDate()将相应地更新Date对象。例如,如果为0 为dayValue提供,日期将设置为最后一天 上个月。
var date = new Date();
date.setDate(now.getDate() - diff);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
var friday = date.getTime();
完整代码
function getLastFridayOf(date) {
var d = new Date(date),
day = d.getDay(),
diff = (day <= 5) ? (7 - 5 + day ) : (day - 5);
d.setDate(d.getDate() - diff);
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
return d.getTime();
}
仅举例来说,它取决于JS引擎,并可能产生不同的结果
new Date(getLastFridayOf('2015-05-01')).toDateString()
-> Fri Apr 24 2015
new Date(getLastFridayOf('2015-05-19')).toDateString()
-> Fri May 15 2015
new Date(getLastFridayOf('2015-05-16')).toDateString()
-> Fri May 15 2015
new Date(getLastFridayOf('2015-05-15')).toDateString()
-> Fri May 08 2015
答案 1 :(得分:7)
-(void)webViewDidFinishLoad:(UIWebView *)webview{
[self performSelector:@selector(checkContentOnDelay:) withObject:webview afterDelay:0.001];
}
-(void)checkContentOnDelay:(UIWebView *)webview{
CGSize contentSize = webview.scrollView.contentSize;
if (contentSize.height > 2) {
[webview updateConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(contentSize.height);
}];
}else{
[self performSelector:@selector(checkContentOnDelay:) withObject:webview afterDelay:0.01];
}
}
&#13;
答案 2 :(得分:0)
这似乎有效,并确保您在当前日期为星期五的上周五仍然可以获得。同时将时间戳放在午夜。
var target = 5, // <- Friday
date = new Date,
days = ( 7 - ( target - date.getDay() ) % 7 ),
time = date.getTime() - ( days * 86400000 );
// setting full timestamp here
date.setTime(time);
// flooring the time to midnight (optional)
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
// date should now hold last Friday
console.log( date.toString() );
值得注意的是,任何JavaScript日期计算都假设用户的计算机设置为正确的日期。所以,不要用于任何关键的事情。
时区也是如此。如果您乐意接受这样的用户时间,那么您可能很高兴它适合自己的时区。只是需要注意的事情。
答案 3 :(得分:0)
const t = new Date().getDate() + (6 - new Date().getDay() - 1) - (new Date().getDay() == 6 ? 0 : 7);
const lastFriday = new Date();
lastFriday.setDate(t);
console.log(lastFriday);
答案 4 :(得分:0)
function lastFriday(){
var date = new Date();
date.setDate(date.getDate()-date.getDay()-2);
return date;
}
答案 5 :(得分:-1)
检查一下,似乎有效,但尚未正确测试
function getLastDayOf(dayOfWeek, date) {
var secondsInDay = 86400;
var startDay = date.getDay();
var daysTillLast = -(startDay - dayOfWeek + 7) % 7;
return new Date(date.getTime() + secondsInDay * daysTillLast );
}
设置日期版本
function getLastDayOf(dayOfWeek, date) {
var startDay = date.getDay();
var daysTillLastFriday = -(startDay - dayOfWeek + 7) % 7;
var lastDay = new Date(date)
lastDay.setDate(date.getDate() + daysTillLastFriday);
return lastDay;
}
示例here
答案 6 :(得分:-1)
function dates(dayOfWeek) { // 0 for sunday, 1 for monday ...
var date = new Date();
date.setDate(date.getDate() + (dayOfWeek - 7 - date.getDay()) % 7);
alert(date);
}
dates(3);
<强> Demo 强>