Perl 将特定数字日期转换为显示月份
例如,要转换该日期:Perl 将特定数字日期转换为显示月份,perl,scripting,sed,awk,tr,Perl,Scripting,Sed,Awk,Tr,例如,要转换该日期: 02082012 In that case: 02 - Day 08 - Month 2012 - Year 目前,我将日期分开,但无法转换为月份: #echo "02082012"|gawk -F "" '{print $1$2 "-" $3$4 "-" $5$6$7$8}' #02-08-2012 转换和捕获所有月份后的预期视图: 02-Aug-2012 直截了当: kent$ date -d "$(echo '02082012'|sed -r 's/(..)(
02082012
In that case:
02 - Day
08 - Month
2012 - Year
目前,我将日期分开,但无法转换为月份:
#echo "02082012"|gawk -F "" '{print $1$2 "-" $3$4 "-" $5$6$7$8}'
#02-08-2012
转换和捕获所有月份后的预期视图:
02-Aug-2012
直截了当:
kent$ date -d "$(echo '02082012'|sed -r 's/(..)(..)(....)/\3-\2-\1/')" "+%d-%b-%Y"
02-Aug-2012
直截了当:
kent$ date -d "$(echo '02082012'|sed -r 's/(..)(..)(....)/\3-\2-\1/')" "+%d-%b-%Y"
02-Aug-2012
我这样想
use 5.10;
use strict;
use warnings;
use Time::Piece;
my $date = '2013-04-07';
my $t = Time::Piece->strptime($date, '%Y-%m-%d');
print $t->month;
print $t->strftime('%Y-%b-%d');
只是我试过这个…我想是这样的
use 5.10;
use strict;
use warnings;
use Time::Piece;
my $date = '2013-04-07';
my $t = Time::Piece->strptime($date, '%Y-%m-%d');
print $t->month;
print $t->strftime('%Y-%b-%d');
我刚刚尝试了这个…另一个Perl解决方案,它包含在Perl核心中的模块中
use POSIX 'strftime';
my $date = '02082012';
print strftime( '%d-%b-%Y', 0, 0, 0,
substr( $date, 0, 2 ),
substr( $date, 2, 2 ) - 1,
substr( $date, 4, 4 ) - 1900 );
请看一个非常好的概述,了解strftime的占位符的作用。另一个Perl模块解决方案,它位于Perl核心中
use POSIX 'strftime';
my $date = '02082012';
print strftime( '%d-%b-%Y', 0, 0, 0,
substr( $date, 0, 2 ),
substr( $date, 2, 2 ) - 1,
substr( $date, 4, 4 ) - 1900 );
请看一篇关于
strftime
占位符的非常好的概述。使用Perl的POSIX模块和strftime
看起来像什么
#! /usr/bin/env perl
use strict;
use warnings;
use POSIX qw/ strftime /;
while (<>) {
chomp;
if (my($d,$m,$y) = /^(\d\d)(\d\d)(\d\d\d\d)$/) {
print strftime("%d-%b-%Y", 0, 0, 0, $d, $m-1, $y-1900), "\n";
}
}
#/usr/bin/env perl
严格使用;
使用警告;
使用POSIXQW/strftime/;
而(){
咀嚼;
如果(my($d,$m,$y)=/^(\d\d)(\d\d)(\d\d\d)$/){
打印strftime(“%d-%b-%Y”,0,0,0,$d,$m-1,$Y-1900),“\n”;
}
}
输出:
$ echo 02082012 | convert-date
02-Aug-2012
$echo 02082012 |转换日期
2012年8月2日使用Perl的POSIX模块和strftime
#! /usr/bin/env perl
use strict;
use warnings;
use POSIX qw/ strftime /;
while (<>) {
chomp;
if (my($d,$m,$y) = /^(\d\d)(\d\d)(\d\d\d\d)$/) {
print strftime("%d-%b-%Y", 0, 0, 0, $d, $m-1, $y-1900), "\n";
}
}
#/usr/bin/env perl
严格使用;
使用警告;
使用POSIXQW/strftime/;
而(){
咀嚼;
如果(my($d,$m,$y)=/^(\d\d)(\d\d)(\d\d\d)$/){
打印strftime(“%d-%b-%Y”,0,0,0,$d,$m-1,$Y-1900),“\n”;
}
}
输出:
$ echo 02082012 | convert-date
02-Aug-2012
$echo 02082012 |转换日期
2012年8月2日要使用固定字段长度拆分字符串,请使用
unpack
:
my $input = "02082012";
my ( $day, $month, $year ) = unpack( 'a2 a2 a4', $input );
print "$input becomes $day, $month, $year\n";
看
然后,如其他答案中所述,使用
POSIX::strftime()
重新格式化日期。要使用固定字段长度拆分字符串,请使用解包
:
my $input = "02082012";
my ( $day, $month, $year ) = unpack( 'a2 a2 a4', $input );
print "$input becomes $day, $month, $year\n";
看
然后,如其他答案中所述,使用POSIX::strftime()
重新格式化日期。是一个核心Perl模块,非常适合这样的简单操作
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
my $string = '02082012';
my $date = Time::Piece->strptime($string, '%d%m%Y');
say $date->strftime('%d-%b-%Y');
(是的,这与user1811486的答案非常相似,但它使用了原始问题中要求的正确格式。)是一个核心Perl模块,非常适合这样的简单操作
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
my $string = '02082012';
my $date = Time::Piece->strptime($string, '%d%m%Y');
say $date->strftime('%d-%b-%Y');
(是的,这与用户1811486的答案非常相似,但它使用了原始问题中要求的正确格式。)您可以这样做:
date-d'08/02/2012'+%d-%b-%Y”
给出了02-Aug-2012
在这种情况下,我得到的输入格式是020812012
是的,我知道了,我得到的问题是“020812012”不是date
的可接受格式。为什么要使用perl标记?这个问题与Perl无关。也许你是对的,但在大多数情况下,我使用Perl是因为尝试学习语言也。。。如果冒犯了您,很抱歉。您可以这样做:date-d'08/02/2012'+%d-%b-%Y”
这会给出02-Aug-2012
在这种情况下,我会得到该格式的输入020812012
是的,我明白了,我遇到的问题是“020812012”不是date
的公认格式。为什么要使用perl标记?这个问题与Perl无关。也许你是对的,但在大多数情况下,我使用Perl是因为尝试学习语言也。。。对不起,我冒犯了你。