Visit Our UNIX and Linux User Community


Converting string to date in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting string to date in perl
# 1  
Old 09-09-2009
Converting string to date in perl

Hi,

I need convert a date string to date.

For eaxmple
$last_date=6/2/2009
and I want to change the format of the above mentioned date to "Jun 2 2009 12:00AM".

Do we have any functionality or staright method to convert to the desired format?
# 2  
Old 09-09-2009
Code:
perl '-MPOSIX qw(strftime)'  -le'
    $last_date = "6/2/2009";
    @dt = split /\//, $last_date;
    print strftime( "%B %d %Y %I:%M%p", 0, 0, 0, $dt[1], $dt[0] - 1,
        $dt[2] - 1900 );
  '

There is also ParseDate from Date::Manip.
# 3  
Old 09-09-2009
Great. It works. Thanks a lot. Can you please describe bit elaborately? I donot understand logic.
# 4  
Old 09-09-2009
Following is the very simple thing to do ...
Code:
use Date::Manip;
print ParseDate("6/3/2009");

# 5  
Old 09-10-2009
Quote:
Originally Posted by siba.s.nayak
Great. It works. Thanks a lot. Can you please describe bit elaborately? I donot understand logic.
Code:
    @dt = split /\//, $last_date; # build an array that contains the date elements:
                                  # + $dt[0] contains the month ... $dt[2] - the year.
    print strftime( "%B %d %Y %I:%M%p", 0, 0, 0, $dt[1], $dt[0] - 1,
        $dt[2] - 1900 );          # the month begin at zero, the year is given in years 
                                  # + since 1900, see perldoc POSIX | less -pstrftime
                                  # + for more information.


Previous Thread | Next Thread
Test Your Knowledge in Computers #1001
Difficulty: Medium
Grace Brewster Murray Hopper was an American computer scientist and United States Navy rear admiral who found a moth in a computer stuck in a relay in 1947 and was later referred to as "Grandma COBOL" for her pioneering work in machine-independent programming languages.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting String Date into UNIX Date

Hi, I have a string date to my unix script(sun solaris). I wanted to convert it into unix date so that I can use it in a conditional statement. Please see below: MyTest.sh -s 2018-05-09 suppdt=$1 # string date passed via arguement as 2018-04-09 curryr=`date '+%Y'` nextyr=`expr... (2 Replies)
Discussion started by: Saanvi1
2 Replies

2. Shell Programming and Scripting

Please help me days to a date string in PERL

Please help me n days to a date string in PERL. Date is of the format YYYYMMDD ---------- Post updated at 08:56 AM ---------- Previous update was at 08:54 AM ---------- Add n days (2 Replies)
Discussion started by: yahoo
2 Replies

3. UNIX for Dummies Questions & Answers

Converting string date time to unix time in AWK

I'd like to convert a date string in the form of sun aug 19 09:03:10 EDT 2012, to unixtime timestamp using awk. I tried This is how each line of the file looks like, different date and time in this format Sun Aug 19 08:33:45 EDT 2012, user1(108.6.217.236) all: test on the 17th ... (2 Replies)
Discussion started by: bkkid
2 Replies

4. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

5. 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

6. Shell Programming and Scripting

Converting date string to different formats

Sucks to be a noob :o See my last post for solution I have 3 different log formats and the filenames contain a date. I am trying to write a script to grep these files between two date ranges. I am now stuck at the date conversion bit. I let the user entered a date string in the format... (6 Replies)
Discussion started by: GermanJulian
6 Replies

7. Shell Programming and Scripting

PERL String to Date (Custom format yyyymmdd to dd-mon-yyyy)

Hi All, I am learning PERL for one of the projects, and in one of these scripts, I read a flat text file and print in the terminal. The problem is, the text file has a date field. The format is yyyymmdd. I need to display this as dd-mon-yyyy. Any ideas to do this? Thanks a lot for the... (9 Replies)
Discussion started by: guruparan18
9 Replies

8. 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

9. HP-UX

a simple way of converting a date in seconds to normal date

Hi all! I'm working on a HPUX system, and I was wondering if there is a simple way to convert a date from seconds (since 1970) to a normal date. Thanks (2 Replies)
Discussion started by: travian
2 Replies

10. Programming

converting character string to hex string

HI Hi I have a character string which contains some special characters and I need it to display as a hex string. For example, the sample i/p string: ץA  g and the o/p should be : D7A5EF4100C5010067EFDBFD Any pointers or sample code pls. (5 Replies)
Discussion started by: axes
5 Replies

Featured Tech Videos