date formatting in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting date formatting in perl
# 1  
Old 11-02-2011
date formatting in perl

my code:
Code:
$dateformat = yyyy_mm_dd
if($dateformat =~ m/yyyy/i)
{
print ("found");
$yyyy = strftime "%Y", localtime;
print ("year is $yyyy\n");
$dateformat =~ s/yyyy/$yyyy/;
print ("new date format is $dateformat\n");
}
elsif($dateformat =~ m/yy/i)
{
print ("found");
$yy = strftime "%y", localtime;
print ("year is $yy\n");
$dateformat =~ s/yy/$yy/;
print ("new date format is $dateformat\n");
}

output = 2011_mm_dd
Is there any method to do this in a simpler way or using sed? Please suggest

Last edited by pludi; 11-02-2011 at 06:45 AM..
# 2  
Old 11-02-2011
Code:
$dateformat = yyyy_mm_dd
if($dateformat =~ m/yyyy/i)
{
print ("found");
$yyyy = strftime "%Y", localtime;
print ("year is $yyyy\n");
$dateformat =~ s/yyyy/$yyyy/;
print ("new date format is $dateformat\n");
}
elsif($dateformat =~ m/yy/i)
{
print ("found");
$yy = strftime "%y", localtime;
print ("year is $yy\n");
$dateformat =~ s/yy/$yy/;
print ("new date format is $dateformat\n");
}

you are replacing the year only. you are replacing the month and date
# 3  
Old 11-02-2011
replacing only the year.
eg: It ll either be "2011" or "11"
# 4  
Old 11-02-2011
Not much simpler, just saves some duplicated code.

Code:
$dateformat = 'yyyy_mm_dd';
$fmt = ($dateformat =~ m/yyyy/i) ? '%Y' : $dateformat =~ m/yy/i ? '%y' : '';

if ($fmt ne '')
{
    print ("found");
    $year = strftime $fmt, localtime;
    print ("year is $year\n");
    $dateformat =~ s/$1/$year/;
    print ("new date format is $dateformat\n");
}

# 5  
Old 11-03-2011
Thanks this works perfect Smilie
can you explain the below syntax and how this works?
Code:
$fmt = ($dateformat =~ m/yyyy/i) ? '%Y' : $dateformat =~ m/yy/i ? '%y' : '';


Last edited by Franklin52; 11-03-2011 at 04:05 AM.. Reason: Please use code tags, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Help with Formatting date in Perl

Good day. I am trying to change the output on my date so that I can get the month as a numeric value. TODAY=`perl -e 'print localtime(time()) . "\n" '` Here are the results from the above perl statement Thu Feb 11 13:16:40 2016 I am not sure of the syntax to get a date more like... (2 Replies)
Discussion started by: ziggy6
2 Replies

2. UNIX for Dummies Questions & Answers

Need Date Formatting help

Hi, How can i store the date + time from the output of the ls command in loop in a variable date1? -rw-rw---- 1 user1 admin 500002 Jan 2 21:24 P002607.cssI then want to convert Jan 2 21:24 to this date format 2014-01-02 21:24:00 and save it in date2 variable. Then i would like to add... (1 Reply)
Discussion started by: mohtashims
1 Replies

3. UNIX for Dummies Questions & Answers

Formatting date time in unix using perl breaks

while read l do vTimeCreated=`perl -e '@d=localtime ((stat(shift))); printf "%02d-%02d-%04d %02d:% 02d:%02d\n", $d,$d+1,$d+1900,$d,$d,$d' ${l}` echo "${l} || ${vTimeCreated}" >> ${fPrefx}_Output_Files_${vDate}.txt done < servername.txt Using the above code to format date time for each of... (5 Replies)
Discussion started by: HeadBang
5 Replies

4. Shell Programming and Scripting

Formatting date

Hi all Y=`date +'%Y'` M=`date +'%m'` D=`date +'%d'` if && ;then yesterday=$Y$M`expr $D + 30` echo $yesterday else if && ; then yesterday=$Y$M`expr $D + 29` echo $yesterday else if ; then yesterday=$Y$M`expr $D + 27` echo $yesterday else yesterday=$Y$M`expr $D - 1` echo... (8 Replies)
Discussion started by: ultimatix
8 Replies

5. Shell Programming and Scripting

Formatting a date

Hi, the date value retrieved by a parameter from the table is of the format dd/mm/yyyy. please let me know how to convert this to YYYYMMDD using sed thanks (4 Replies)
Discussion started by: swasid
4 Replies

6. OS X (Apple)

Date Formatting, etc.

Hi - I'm using GeekTool to customize my desktop in OS X 10.5.8 I'm a complete novice as far as UNIX commands, just know enough to be dangerous. I have a command entered as a Shell to display my events from iCal: This makes my events show something like this: While this is... (1 Reply)
Discussion started by: patricksprague
1 Replies

7. Shell Programming and Scripting

date formatting

Hi i need to have the date in the format like dd-mon-yyyy my script goes like this #!/usr/bin/bash for f in /space/can /home/lbs/current/externalcdrbackup/L_CDR_Configuration/1/200903122* ; do awk '{sum++;}END{for(i in sum) {print d,h,m,i, sum}}' "d=$(date +'%m-%d-%Y')" "h=$(date +'%H')"... (8 Replies)
Discussion started by: aemunathan
8 Replies

8. UNIX for Dummies Questions & Answers

Date formatting

Running bash how do I input the date in the command line like 3/20/90 and get an output formmated like March, 20 1990. (8 Replies)
Discussion started by: knc9233
8 Replies

9. Shell Programming and Scripting

date formatting

Date format MM/DD/YYYY required is YYYYMMDD, I tried using sed but could not get it any help please. (4 Replies)
Discussion started by: mgirinath
4 Replies

10. Shell Programming and Scripting

Formatting date

i need date in the following format December 14, 2005. With date +"%b %d, %Y" command i am getting the following output :- Dec 14, 2005. can anyone pls tell me how to get the full month name (2 Replies)
Discussion started by: radhika03
2 Replies
Login or Register to Ask a Question