我如何找到当前日期是否在另外两天之间。
如果另外两天是周二至周四,则以下代码有效
但是如果我想让我的用户给出一个像周五到周一这样的范围怎么办。
my $timestamp = time;
my $dow = ( localtime($timestamp) )[6];
my $range = '5-1'; # 5 being friday and 1 being monday
$range =~ m/(\d+)-(\d+)/;
my ( $start, $end ) = ( $1, $2 );
if ( $start >= 0 && $end <= 6 && $dow >= $start && $dow <= $end ) {
print "$dow falls between your range";
}
最佳答案
my $in = ( $start <= $end
? $start <= $dow && $dow <= $end
: !( $end < $dow && $dow < $start )
);
或者
my $in = ( $start <= $end
? $start <= $dow && $dow <= $end
: $dow <= $end || $start <= $dow
);
关于Perl:检查当前日期是否在给定的两天之间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24482784/