Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Date format

by jsuresh (Novice)
on Jun 11, 2018 at 11:40 UTC ( #1216380=perlquestion: print w/replies, xml ) Need Help??

jsuresh has asked for the wisdom of the Perl Monks concerning the following question:

Hi Team,

I'm giving an excel sheet as an input to a perl file. In that, one of the column is date. The date format in that excel sheet is "21/03/2018" but when I upload it as input using perl, the format changes to "2018-03-21".

Another thing is , the format of the date in the input has "/" between them but after uploading it into perl, it changes to "-".

PleASE SUPPORT.

CODE

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"; if ($origdate =~ /\//) { print "test\n"; if($dateformat eq 'mm/dd/yyyy') { my $dateparts = [ split /\//, $origdate ]; if (length($dateparts->[0])<2) {$dateparts->[0]= "0".$d +ateparts->[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 ),$date +parts->[1],( $dateparts->[2] - 1900 )); } else{ ($mon,$mday,$year)=(($dateparts->[0] - 1 ),$date +parts->[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]),$datepart +s->[1] - 1,($dateparts->[2] - 1900)); } else{ ($mday,$mon,$year) =(($dateparts->[0]),$datepar +ts->[1] - 1,($dateparts->[2] + 100)); }
tHANKS, JP

2018-06-12 Athanasius added code tags and removed paragraph tags from within code

Replies are listed 'Best First'.
Re: Date format
by thanos1983 (Parson) on Jun 11, 2018 at 13:34 UTC

    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:

    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!
      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;
Re: Date format
by runrig (Abbot) on Sep 06, 2018 at 17:56 UTC
    I do not understand what your issue actually is, but,
    1) 'YYYY-MM-DD' is the preferred default date format,
    2) Excel date cells can be formatted using the ExcelFmt function in the Spreadsheet::ParseExcel::Utility library, and
    3) The Oracle default date/timestamp formats can be set like so (notice I set it to my preferred format, and I do this for every connection in my connect wrapper library):
    $dbh->do("alter session set nls_date_format='YYYY-MM-DD HH24:MI: +SS'"); $dbh->do("alter session set nls_timestamp_format='YYYY-MM-DD HH2 +4:MI:SS.FF'"); $dbh->do("alter session set nls_timestamp_tz_format='YYYY-MM-DD +HH24:MI:SS.FF'");
    Update: Also note that even though the Oracle format includes a time component, it will still accept a string without the time in the format 'YYYY-MM-DD'.
Re: Date format
by poj (Abbot) on Jun 11, 2018 at 12:15 UTC
    giving an excel sheet as an input to a perl file
    when I upload it as input using perl

    You need to show the code (in code tags please) for these parts of your program.

    poj
Re: Date format
by Anonymous Monk on Jun 12, 2018 at 16:10 UTC
    The Perl language is bristling with time-handling modules, both within the core of the language and otherwise. You certainly do not need to write your own code to pick-apart a well-formed date string. Create a date/time object, tell it to parse the string you have. When you're ready to spit out the date, tell the object to give it to you in string form (according to the format of your choice). See e.g. "Time::Piece."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1216380]
Approved by Marshall
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2021-05-09 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (100 votes). Check out past polls.

    Notices?