in reply to Date format

Hello jsuresh,

From the sample of code that you provide us you have only one return function on if none in else, also you are missing a few closing brackets on else.

Also why you are doing so many date calculations? There is no date module that can do everything for you?

What is exactly the input and output that you are trying to get?

Provide us a sample of compiling code so we can help you.

Update: After a lot of modifications on your sample code in order to make it able to compile I get the following error:

#!/usr/bin/env perl use strict; use warnings; use Time::Local; sub Date_Conversion_Julian{ my $origdate = shift; # my $date= Time::Piece->strptime($origdate$, '%Y-%m-%d'); #print $date->strftime('%d/%m/%Y'),"\n"; print "d:$origdate\n"; my $dateformat = "mm/dd/yyyy"; if ($origdate =~ /\//) { print "test\n"; if($dateformat eq 'mm/dd/yyyy') { my @dateparts = split /\//, $origdate ; if (length($dateparts[0])<2) { $dateparts[0]= "0".$dateparts[0]; } if (length($dateparts[1])<2) { $dateparts[1]= "0".$dateparts[1]; } my ($mon,$mday,$year ); if(length($dateparts[2]) > 2) { ($mon,$mday,$year) = ( ($dateparts[0] - 1 ), $dateparts[1], ( $dateparts[2] - 1900 ) ); } else{ ($mon,$mday,$year) = ( ($dateparts[0] - 1 ), $dateparts[1], ($dateparts[2] + 100 ) ); } my ( $sec, $min, $hours ) = ( "00", "00", "12" ); my $julian_date = timelocal($sec, $min, $hours, $mday, $mon, $year); $julian_date = ($julian_date / 86400) + 2440588; return $julian_date; } else { my @dateparts = split /\//, $origdate ; if (length($dateparts[0])<2) { $dateparts[0]= "0".$dateparts[0]; } if (length($dateparts[1])<2) { $dateparts[1] = "0".$dateparts[1]; } #my @dateparts = $dateparts; #print Dumper \@dateparts; my ($mon,$mday,$year ); if(length($dateparts[2]) > 2) { ($mday,$mon,$year) = ( ($dateparts[0]), $dateparts[1] - 1, ($dateparts[2] - 1900) ); } else{ ($mday,$mon,$year) = ( ($dateparts[0]), $dateparts[1] - 1, ($dateparts[2] + 100) ); } } } } print Date_Conversion_Julian("21/03/2018"); __END__ $ perl test.pl d:21/03/2018 test Month '20' out of range 0..11 at test.pl line 42.

I had to add the missing module, missing curly brackets, convert your hash references to array elements and after that all your wrong hash reference points to array variables. Even after doing all that your code still does not compile as your calculations are wrong.

So try to explain us, you are trying to produce a date e.g. today (11/06/2018) then what? Are you trying to convert the (/) to (-) and push this date to excel? Are you trying to do some date calculations? Give us more information so we can try to help you.

Looking forward to your udpate, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Date format
by jsuresh (Acolyte) on Sep 06, 2018 at 08:33 UTC
    HI,

    I have finally found the exact problem. The date which I get is in the format "02/08/2018" but whereas the needed format is "2/8/2018". How can I get this in perl?

    Do i need to convert it or is there any other way to remove the leading zero from the date. I want to eliminate the leading zero either from the month or date if the date/month is single digit, I have already tried with various ideas, but it is not working, Kindly support.

    JP

      So, what ideas did you try and how did they fail?

      You can help us help you better by showing us the code you have tried and telling us how it failed for you. That allows us to help you much better clear up your misunderstandings.

      For example, you could start with this program:

      #!perl use strict; use warnings; use Test::More; my $input_value = '02/08/2018'; my $desired_value = '2/8/2018'; # do stuff with input value: my $transformed_value = $input_value; $transformed_value =~ s!0!!g; # remove all zeroes is $transformed_value, $desired_value, "Conversion strips leading zero +es"; done_testing;
        Hi,

        I tried to change the format of the date while fetching it from the DB but, I'm unable to do so, it is oracle DB.

        # execute the sql query my $sth1 = $dbh->prepare($sql1); my $sth2 = $dbh->prepare($sql2); $sth1->execute(); $sth2->execute(); # write the data into worksheet my $row1=1; while(my @field=$sth1->fetchrow_array){ my $col1=0; $field[8]=$priority{$field[8]}; $field[7]=$priority{$field[7]}; $field[33]=$statusyn{$field[33]}; $field[17]=$status{$field[17]}; $field[14]=$statusyn{$field[14]}; $field[44]=$tkttype{$field[44]}; $field[32]=$statusyn{$field[32]}; my $arr_ref=\@field; # my $date = $arr_ref->[$datecol]; # $date =~ s/ //; $worksheet1->write_row($row1,0,$arr_ref); $row1++; }

        The date is one of the column in my expoerted data. And it is of the format "06-05-2018" but I need it to be '6-5-2018'.

        How can I proceed to change the format to my desired format once after the code is fetched from DB?

        Thanks,

        JP

        2018-09-08 Athanasius added code tags