我试图计算并以分钟显示输入日期和当前时间之间的差异。我有:
print "Enter a date YYYY MM DD. Remember perl's months go from 0-11.\n";
while ( @dateEnt < 1 ) {
my $dateEntered = <STDIN>;
chomp $dateEntered;
push @dateEnt, $dateEntered;
(@datedata) = split( /\s+/, $dateEntered );
$year = $datedata[0];
$month = $datedata[1];
$day = $datedata[2];
}
$time = time;
$readabletime = localtime($time);
use Time::Local;
$timeB = localtime [1];
$timeBetween = $readabletime[1] - $timeB;
print "The time between the dates is $timeBetween\n";
当我执行此操作时,我得不到答案。有帮助吗?
答案 0 :(得分:3)
让我们来看看最后一个块
use Time::Local;
$timeB = localtime[1];
$timeBetween = $readabletime[1] - $timeB;
print "The time between the dates is $timeBetween\n";
use
语句通常放在程序的顶部,因为它实际上是在编译时执行的,但它真的很好
localtime[1]
看起来像是在尝试访问名为localtime
的数组的第二个元素(Perl数组从索引零开始)。但是你实际上在做的是创建一个匿名数组,它包含单个值1并将对它的引用传递给localtime
。如果您尝试使用print[1]
,那么您会看到ARRAY(0x22c4000)
之类的内容。所以localtime
正在尝试将内存地址转换为日期和时间
$readabletime[1]
正在尝试访问数组@readabletime
的第二个元素,该元素不存在,因此它将评估为undef
您最终会尝试从"Thu Mar 4 18:50:24 1971"
中删除 string undef
之类的内容,这是毫无意义的。我认为你应该得到类似
Argument "Thu Mar 4 18:50:24 1971" isn't numeric in subtraction
答案 1 :(得分:3)
use strict;
和use warnings;
会告诉你很多问题。
while
循环是多余的,因为您push
没有任何验证。因此,在第二次迭代中,总是为真。 @dateEnt
多余。localtime
给出一个字符串。你不能用字符串做数学。 (有时你可以作弊,因为perl
可以将它转换为数字,但这不适用于日期字符串。)use
通常位于程序的顶部,因为无论localtime
返回值数组。 {em>可以 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
(localtime)[1]
$min
。$readabletime[1]
但无论如何,这对于比较来说并没有太大意义。 @readabletime
指的是名为Time::Piece
的数组的第二个元素。这不存在。 所以要完成你所追求的目标 - 我建议使用#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece
my $date;
while ( not $date ) {
print "Enter a date YYYY MM DD\n";
my $dateEntered = <STDIN>;
chomp $dateEntered;
$date = Time::Piece->strptime( $dateEntered, "%Y %m %d" );
}
print "Understood: $date\n";
my $timebetween_s = ( time() - $date->epoch );
print $timebetween_s, "s between then and now\n";
(核心perl模块),如下所示:
strptime
注意 - 它使用Time::Piece
将输入的日期转换为print
对象(如果需要,您只需strptime
并获得字符串化日期,但您也可以执行其他操作变换)。 $date->epoch
支持许多不同的格式。
但你只需减去time()
- 从1970年1月1日起的秒数,从function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("PRECLOSING");
var range = sheet.getRange("E:E,I:J,M:M,P:P,S:V,AB:AC,AF:AF");
range.activate();
var values = rangeY.getValues();
//for each row that data is present
for(var i = 0; i < values.length; i++) {
var cell = sheet.getRange(i + 1, 2);
if ( values == "X" )
{
cell.setBackground('black');
return;
} else {
cell.setBackground('white');
}
}
}
开始,这也是自1970年1月1日以来的秒数。这给你一个像你一样的时间增量想要 - 在几秒钟内。如果你想要不同的单位,你需要划分它。
答案 2 :(得分:1)
你可以试试这个
use strict;
use warnings;
use DateTime;
my $from_date = DateTime->new(
year => 2014,
month => 9,
day => 24,
hour => 9,
minute => 13,
second => 8,
time_zone => 'local',
);
print "$from_date\n";
my $current_date = DateTime->now( time_zone => 'local' )->set_time_zone('floating');
print "$current_date\n";
my $time_diff = $current_date - $from_date;
print 'year = ', $time_diff->years, "\n";
print 'month = ', $time_diff->months, "\n";
print 'days = ', $time_diff->days, "\n";
print 'hours = ', $time_diff->hours, "\n";
print 'minutes = ', $time_diff->minutes, "\n";
print 'secondss = ', $time_diff->seconds, "\n";
结果:
2014-09-24T09:13:08
2015-09-25T21:31:37
year = 1
month = 0
days = 1
hours = 12
minutes = 18
secondss = 29