Date format in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date format in Perl
# 1  
Old 06-02-2008
Question Date format in Perl

How do I display the date in the format "YYYY-Mmm-DD" using perl.

e.g 2008-Jun-03

I have a perl command below which displays the date in the format "YYYY-MM-DD" but I want the month to be displayed as "Mmm" (The first 3 characters of the name of the month with the initial letter being upper case)

Code:
perl -e '@d=localtime ((stat(shift))[9]); printf "%4d-%02d-%02d\n", $d[5]+1900,$d[4]+1,$d[3]'

Steve
# 2  
Old 06-03-2008
There's no built-in formatting command for that, but it's not hard to roll your own.

Code:
@mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
printf "%4d-%s-%02d\n", $d[5]+1900, $mon[$d[4]], $d[3];

# 3  
Old 06-03-2008
You may install Date::Format if you prefer a more flexible way of formatting dates.

Date::Format - Date formating subroutines - search.cpan.org
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl one-liner to get yesterday's date in format dd-MMM-yy (i.e. 01-JAN-12)

I have the following perl one-liner to get yesterday's date, but I would like it in the form of dd-MMM-yy (for example: 01-JAN-12). Can someone alter the below code so I get the format I want? Also, could someone also give me a line for dd-Mmm-yy (for example 01-Jan-12)? Code: YEST=`perl -w... (3 Replies)
Discussion started by: thibodc
3 Replies

2. Shell Programming and Scripting

Validating date in yyyymmdd format using PERL

Hi all, i had a code where in user will enter a date in yyyymmdd format.. i didnt use any validation for the date and now the problem is if a user enters date instead of month after year it is proceeding with the code.. like if the date is 20120426 and if the user enters 20122604 it... (4 Replies)
Discussion started by: smarty86
4 Replies

3. Shell Programming and Scripting

PERL : hhmiss - Date format check and replace

I have a filename, This can be any of any format, I want to check if the filename has hours,mins and seconds part. If it is present, i want to replace it with a " * " (star symbol) output needed: IMP: The time part can be in any pattern. How can this be done?:confused:... (3 Replies)
Discussion started by: irudayaraj
3 Replies

4. Shell Programming and Scripting

Date format check and replace string in PERL

I have got few date format patterns like "yyyymmdd", "yy_mm_dd" etc. There can be any combination of such patterns. I have used add_delta_days to find "yyyy", "yy", "mm", "dd" for the current date and saved them to different variables like "$y1", "$y2", "$m1" etc In one line, i want to... (10 Replies)
Discussion started by: irudayaraj
10 Replies

5. Shell Programming and Scripting

Different types of Date format in perl

I want to know the different types of date format possible in perl. Eg: yyyymmdd, yymmdd etc...Is there any format like YMD? Where can i find the list of all possibilites? Thanks in advance (1 Reply)
Discussion started by: irudayaraj
1 Replies

6. Programming

Basic perl code- Date format

Hi friends, Please see the below code carefully. ======================================================= # Get batch date and Ord range open OR,$ARGV; while (<OR>) { # find the batch date next if length $_ < 3; # BLANK LINE # last if $. > 120; # sample should be good enough... (2 Replies)
Discussion started by: pspriyanka
2 Replies

7. Shell Programming and Scripting

perl regular expression format date

Hi Everyone, Mon 18 Jan 2010 09:52:10 AM MYT the output is 20100118 09:52:10 how to do it in perl regular expression =~ Thanks (3 Replies)
Discussion started by: jimmy_y
3 Replies

8. UNIX for Dummies Questions & Answers

perl yesterday's date in mmddyyyy format

Hi, Below command is producing yesaterday's date in mmddyy format - perl -e '@T=localtime(time-86400);printf("%02d%02d%02d",$T+1,$T,($T+1900)%100)' But i want the date in mmddyyyy format; plz help. Thankx, Rahul Bahulekar. ---------- Post updated at 05:13 AM ---------- Previous... (1 Reply)
Discussion started by: rahulbahulekar
1 Replies

9. Shell Programming and Scripting

20090620231013 to date format i am using substr, any simple way in perl?

Hi Everyone, $tmp="20090620231013"; $tmp = substr($tmp,0,8)." ".substr($tmp,8,2).":".substr($tmp,10,2).":".substr($tmp,12,2); So my output is: 20090620 23:10:13. I only can think substr is easy, any perl can do this just one line very simple efficient one? :eek: Thanks (3 Replies)
Discussion started by: jimmy_y
3 Replies

10. Shell Programming and Scripting

How to search a date format from a file an replace with a string in PERL

I am very new to Perl. I am struggling so hard to search a date (such as 10/09/2009, 10-09-2009) from a text file and replace with a string (say DATE) using Perl. Please help me out. Thanks in advance. Regds Doren (4 Replies)
Discussion started by: my_Perl
4 Replies
Login or Register to Ask a Question