perl 字符串转换为日期
答案:2 悬赏:50 手机版
解决时间 2021-03-23 09:16
- 提问者网友:十年饮冰
- 2021-03-22 10:20
现在有这样的需求:命令行参数接收如‘20140225’这样的字符串参数,我怎样取出它的前一天、后一天?localtime能实现吗?
最佳答案
- 五星知识达人网友:行路难
- 2021-03-22 11:53
用Date::Calc包可以实现。
方法清单:
Days_in_Year
Days_in_Month
Weeks_in_Year
leap_year
check_date
check_time
check_business_date
Day_of_Year
Date_to_Days
Day_of_Week
Week_Number
Week_of_Year
Monday_of_Week
Nth_Weekday_of_Month_Year
Standard_to_Business
Business_to_Standard
Delta_Days
Delta_DHMS
Delta_YMD
Delta_YMDHMS
N_Delta_YMD
N_Delta_YMDHMS
Normalize_DHMS
Add_Delta_Days
Add_Delta_DHMS
Add_Delta_YM
Add_Delta_YMD
Add_Delta_YMDHMS
Add_N_Delta_YMD
Add_N_Delta_YMDHMS
System_Clock
Today
Now
Today_and_Now
This_Year
Gmtime
Localtime
Mktime
Timezone
Date_to_Time
Time_to_Date
Easter_Sunday
Decode_Month
Decode_Day_of_Week
Decode_Language
Decode_Date_EU
Decode_Date_US
Fixed_Window
Moving_Window
Compress
Uncompress
check_compressed
Compressed_to_Text
Date_to_Text
Date_to_Text_Long
English_Ordinal
Calendar
Month_to_Text
Day_of_Week_to_Text
Day_of_Week_Abbreviation
Language_to_Text
Language
Languages
Decode_Date_EU2
Decode_Date_US2
Parse_Date
ISO_LC
ISO_UC
其中,
Add_Delta_Days
=>
($year,$month,$day) =
Add_Delta_Days($year,$month,$day, $Dd);
这个方法应该能够满足要求
方法清单:
Days_in_Year
Days_in_Month
Weeks_in_Year
leap_year
check_date
check_time
check_business_date
Day_of_Year
Date_to_Days
Day_of_Week
Week_Number
Week_of_Year
Monday_of_Week
Nth_Weekday_of_Month_Year
Standard_to_Business
Business_to_Standard
Delta_Days
Delta_DHMS
Delta_YMD
Delta_YMDHMS
N_Delta_YMD
N_Delta_YMDHMS
Normalize_DHMS
Add_Delta_Days
Add_Delta_DHMS
Add_Delta_YM
Add_Delta_YMD
Add_Delta_YMDHMS
Add_N_Delta_YMD
Add_N_Delta_YMDHMS
System_Clock
Today
Now
Today_and_Now
This_Year
Gmtime
Localtime
Mktime
Timezone
Date_to_Time
Time_to_Date
Easter_Sunday
Decode_Month
Decode_Day_of_Week
Decode_Language
Decode_Date_EU
Decode_Date_US
Fixed_Window
Moving_Window
Compress
Uncompress
check_compressed
Compressed_to_Text
Date_to_Text
Date_to_Text_Long
English_Ordinal
Calendar
Month_to_Text
Day_of_Week_to_Text
Day_of_Week_Abbreviation
Language_to_Text
Language
Languages
Decode_Date_EU2
Decode_Date_US2
Parse_Date
ISO_LC
ISO_UC
其中,
Add_Delta_Days
=>
($year,$month,$day) =
Add_Delta_Days($year,$month,$day, $Dd);
这个方法应该能够满足要求
全部回答
- 1楼网友:时间的尘埃
- 2021-03-22 13:32
你的字符串是如何存储的? \ 这个符号,在字符串中应该是转义的\\表示;除非你用的是''。下面的代码用的是字符串替换法:
#my $str = "c:\\program files\\installshield installation information\\{bf06680-3bbafee5524c}\\abc.pl";
my $str = 'c:\program files\installshield installation information\{bf06680-3bbafee5524c}\abc.pl';
$str =~ s{.*\\}{};
$str =~ s{\.[^.]+$}{};
print "$str\n";结果:
abc" "字符串中如果没用转义,会报错,但依然有输出正确的结果。
如果要得到自身的名称,不同的perl系统,结果不同。 windows下activeperl $0 带全路径,而strawberry则没有。 你可以用中性的方法:
use cwd 'abs_path';
print abs_path($0); 嗯,我没理解你上面的描述,你不要$0的路径,只要文件名,如下:
my $str2 = abs_path($0);
$str2 =~ s{.*\\}{};
print "$str2\n"你如果用activeperl,可以直接试试:
my $str2 = $0;
$str2 =~ s{.*\\}{};
print "$str2\n"
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯