Date Functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sri12
    New Member
    • Sep 2006
    • 1

    Date Functions

    Hi ,

    I want to convert a Date in any format into any other format i required.
    Is there any built in function in PERL to do the date conversion in required format.


    For exampe: If date is in 28-09-2006 i want to convert it to 09-22-2006 i.e.,(dd-mm-yyyy to mm-dd-yyyy)


    Mainly i want to convert date in the format 28-sep-2006 to 09-22-2006 1.e.,(dd-mm-yyyy to mon-dd-yyyy).

    Means i want to know is there any built in function in PERL like to_date function in ORACLE.
  • deep022in
    New Member
    • Sep 2006
    • 23

    #2
    Originally posted by sri12
    Hi ,

    I want to convert a Date in any format into any other format i required.
    Is there any built in function in PERL to do the date conversion in required format.


    For exampe: If date is in 28-09-2006 i want to convert it to 09-22-2006 i.e.,(dd-mm-yyyy to mm-dd-yyyy)


    Mainly i want to convert date in the format 28-sep-2006 to 09-22-2006 1.e.,(dd-mm-yyyy to mon-dd-yyyy).

    Means i want to know is there any built in function in PERL like to_date function in ORACLE.

    =============== ==========
    hi,
    Use the time function to get current times in terms of seconds.
    1 .$time_v = time;
    Pass this result to gmtime function.
    the syntax is as displayed below.
    2 . ($sec,$min,$hou r,$mday,$mon,$y ear,$wday,$yday ,$isdst) = gmtime($time_v) ;

    Converts a time as returned by the time function to an 9-element list with the time localized for the standard Greenwich time zone. Typically used as follows:
    All list elements are numeric, and come straight out of the C `struct tm'. $sec, $min, and $hour are the seconds, minutes, and hours of the specified time. $mday is the day of the month, and $mon is the month itself, in the range 0..11 with 0 indicating January and 11 indicating December. $year is the number of years since 1900. That is, $year is 123 in year 2023. $wday is the day of the week, with 0 indicating Sunday and 3 indicating Wednesday. $yday is the day of the year, in the range 0..364 (or 0..365 in leap years). $isdst is always 0 .

    Note that the $year element is not simply the last two digits of the year. If you assume it is then you create non-Y2K-compliant programs--and you wouldn't want to do that, would you?

    The proper way to get a complete 4-digit year is simply:
    $year += 1900;
    $month=$month+1 ; # since months are represeneted like 0 to 11

    finally
    3. use sprint function to format the date according to your wish


    let me know if you still faces difficulties.

    Comment

    • miller
      Recognized Expert Top Contributor
      • Oct 2006
      • 1086

      #3
      Code:
      #!/usr/bin/perl
      
      use Date::Format;
      use Date::Parse;
      
      foreach my $date ('28-09-2006', '28-sep-2006') {
      	my $newDate = $oldDate;
      	$newDate =~ s/^(\d{2})-(\d{2})-(\d{4})$/$2-$1-$3/; # Only include this line if you are assuming european style dates.  Ie, the day of the month number comes before the month number
      	my @dateArray = strptime($newDate);
      	$newDate = strftime("%m-%d-%Y", @dateArray);
      	
      	print "Old Date = '$date' .... New Date = '$newDate'\n";
      }
      
      1;
      
      __END__
      The solution to your problem can be found in two CPAN modules, namely Date::Parse and Date::Format. As can be derived from their names, one is for parsing randomly formatted dates and the other is for formatting dates.

      The CPAN documentation is pretty self explanatory, but unfortunately your requested example poses a problem. The Parse module assumes that numerical dates are in americanized order. Namely that the month comes before the day of month. The only way to fix this is to use a regular expression to switch the order. Obviously, you would not want to use this regular expression on any dates that were already in order, so some knowledge of the data that you are using is required in advance.

      Comment

      Working...