473,321 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Display dates between range in perl

Hi ,

i want a sample perl code to display all the dates between a date range.
say, the start_date=18/03/2007 and end_date=18/04/2007.
here i want to print all the dates beween 18/03/2007 & 18/04/2007 in perl.

Plesae help at the earliest.
Oct 17 '07 #1
12 8348
numberwhun
3,509 Expert Mod 2GB
Hi ,

i want a sample perl code to display all the dates between a date range.
say, the start_date=18/03/2007 and end_date=18/04/2007.
here i want to print all the dates beween 18/03/2007 & 18/04/2007 in perl.

Plesae help at the earliest.
One of the easiest ways that I know of to compare two dates is to convert them into epoch seconds ( the number of seconds since January 1st, 1970, when Unix was born).

You can check out how to do this by reading up on the DateTime::Format::Epoch module.

Regards,

Jeff
Oct 17 '07 #2
jeff,
i need to diaply all the dates between the date range in the same format in perl.
could you plesae gimme the code here. i need it urgent.
Oct 17 '07 #3
numberwhun
3,509 Expert Mod 2GB
jeff,
i need to diaply all the dates between the date range in the same format in perl.
could you plesae gimme the code here. i need it urgent.
I completely sympathize with your urgency, but please know that this is not a code writing service, and your emergencies are not ours. Everyone here volunteer their time. This being a learning site, we require that you do the legwork and we will help you if you get stuck.

Go ahead and try some code and see if you can get it to work. Doing the conversion and comparison is pretty easy.

Regards,

Jeff
Oct 17 '07 #4
i got the below error when i try to run the sample scrpt
Can't locate DateTime/Format/Epoch.pm in @INC (@INC contains: /usr/perl5/5.6.1/lib/sun4-solaris-64i
nt /usr/perl5/5.6.1/lib /usr/perl5/site_perl/5.6.1/sun4-solaris-64int /usr/perl5/site_perl/5.6.1 /u
sr/perl5/site_perl /usr/perl5/vendor_perl/5.6.1/sun4-solaris-64int /usr/perl5/vendor_perl/5.6.1 /us
r/perl5/vendor_perl .) at d.pl line 2.
BEGIN failed--compilation aborted at d.pl line 2.
Oct 17 '07 #5
numberwhun
3,509 Expert Mod 2GB
i got the below error when i try to run the sample scrpt
Can't locate DateTime/Format/Epoch.pm in @INC (@INC contains: /usr/perl5/5.6.1/lib/sun4-solaris-64i
nt /usr/perl5/5.6.1/lib /usr/perl5/site_perl/5.6.1/sun4-solaris-64int /usr/perl5/site_perl/5.6.1 /u
sr/perl5/site_perl /usr/perl5/vendor_perl/5.6.1/sun4-solaris-64int /usr/perl5/vendor_perl/5.6.1 /us
r/perl5/vendor_perl .) at d.pl line 2.
BEGIN failed--compilation aborted at d.pl line 2.
In order to use the module, you are going to have to install it on your system as it is not part of the default Perl install.

You will want to do something like the following:

Expand|Select|Wrap|Line Numbers
  1. perl -MCPAN -e 'install DateTime::Format::Epoch'
  2.  
That will install the module if you already have the CPAN interface setup. If not, you may have to back out, setup the CPAN interface, install the Bundle::CPAN module, and then install this one.

Regards,

Jeff
Oct 17 '07 #6
KevinADC
4,059 Expert 2GB
jeff,
i need to diaply all the dates between the date range in the same format in perl.
could you plesae gimme the code here. i need it urgent.

Why is it urgent?
Oct 17 '07 #7
numberwhun
3,509 Expert Mod 2GB
Why is it urgent?
I don't think it matters. Especially since his sense of urgency does not translate to us. :-)
Oct 17 '07 #8
KevinADC
4,059 Expert 2GB
I don't think it matters. Especially since his sense of urgency does not translate to us. :-)
I would like to read the explanation why this is urgent to the OP.
Oct 17 '07 #9
numberwhun
3,509 Expert Mod 2GB
I would like to read the explanation why this is urgent to the OP.
LOL!!! See, now your just pickin'!!
Oct 17 '07 #10
Hi,

Use the below script !!!!!!
Expand|Select|Wrap|Line Numbers
  1. # =======================
  2. #!/usr/bin/perl
  3.  
  4. #
  5. # ==> One limitation: This script is not tested for dates before 1st Jan 1970 <==
  6. #
  7. my( $fromdate, $todate , $fromep, $toep) ;
  8.  
  9. if ($#ARGV < 1)
  10.    { print STDERR "Script $0 must get 2 input parameters\n"; &usage ; exit(1) ;}
  11.  
  12. $fromdate = $ARGV[0] ;
  13. $todate   = $ARGV[1] ;
  14.  
  15. if ( $fromdate > $todate )
  16. {
  17.    print STDERR "From date must be less than or equal to To-date. Both dates must be in YYYYMMDD format\n"; &usage; exit(1); }
  18.  
  19. $_ = $fromdate ;
  20. if (!/([0-9]){8}/)
  21. {  print STDERR "Check if From-date is in YYYYMMDD format\n"; &usage; exit(1); }
  22.  
  23. $_ = $todate ;
  24. if (!/([0-9]){8}/)
  25. {  print STDERR "Check if To-date is in YYYYMMDD format\n"; &usage; exit(1); }
  26.  
  27. if ( $fromdate > 20381231 || $todate > 20381231 )
  28. { print STDERR "\n\nThis script will not work for dates greater than 31st December 2038\nSorry about that, please use alternative logic\n\n" ; exit(1);}
  29.  
  30. if ( $fromdate < 19700101 || $todate < 19700101 )
  31. { print STDERR "\n\nThis script will not work for dates before 1st January 1970\nSorry about that, please use alternative logic\n\n" ; exit(1);}
  32.  
  33. &showDaysBetween ( $fromdate , $todate ) ;
  34.  
  35.  
  36. sub showDaysBetween {
  37.     my( $fromdate, $todate , $fromep, $toep, $currdate, $currep) ;
  38.     ( $fromdate, $todate) = @_ ;
  39.  
  40.     $fromep = &getEpochForGivenDate( "$fromdate" ) ;
  41.     # print "Epoch for $fromdate is $fromep\n" ;
  42.     $currdate = $fromdate ;
  43.     $currep = $fromep ;
  44.  
  45.     $toep = &getEpochForGivenDate( "$todate" ) ;
  46.     # print "Epoch for $todate is $toep\n" ;
  47.  
  48.     print "Day = $currdate\n" ;
  49.     while ( $currdate != $todate )
  50.         {
  51.            $currep += 86400 ;
  52.            $currdate = &getYYYYMMDDforEpoch( $currep ) ;
  53.            print "Day = $currdate\n" ;
  54.         }
  55. }
  56.  
  57. ## Get the epoch value for given day. Current system date is the reference for this.
  58. ## We can get the epoch for current date-time using the time() function, the by adding or substracting 86400 seconds
  59. ## (i.e. seconds in one day) we will find out the epoch number for any given day
  60. sub getEpochForGivenDate {
  61.     my ($indate, $today, $day, $month, $year, $nowtime, $newday, $newtime) ;
  62.     ($indate) = @_ ; ## Input date must be in YYYYMMDD format
  63.  
  64.     $nowtime = time ;
  65.     ($day, $month, $year) = (localtime($nowtime))[3,4,5] ;
  66.     $year = $year + 1900 ; $month ++ ;
  67.     $today="$year$month$day" ;
  68.  
  69.     if ( $indate == $today )
  70.          { retuen $nowtime ; }
  71.     elsif ( $indate lt $today )
  72.          {
  73.            $newtime = $nowtime - 86400 ;
  74.            $newday = &getYYYYMMDDforEpoch( $newtime ) ;
  75.            print "Indate=$indate Newday=$newday\n" if ($ENV{DETAIL_DEBUG} eq "1") ;
  76.            while ( $newday != $indate )
  77.                  {
  78.                    $newtime = $newtime - 86400 ;
  79.                    $newday = &getYYYYMMDDforEpoch( $newtime ) ;
  80.                    print "Indate=$indate Newday=$newday\n" if ($ENV{DETAIL_DEBUG} eq "1") ;
  81.                  }
  82.            return $newtime ;
  83.          }
  84.     else
  85.          {
  86.            $newtime = $nowtime + 86400 ;
  87.            $newday = &getYYYYMMDDforEpoch( $newtime ) ;
  88.            print "Indate=$indate Newday=$newday\n" if ($ENV{DETAIL_DEBUG} eq "1") ;
  89.            while ( $newday != $indate )
  90.                  {
  91.                    $newtime = $newtime + 86400 ;
  92.                    $newday = &getYYYYMMDDforEpoch( $newtime ) ;
  93.                    print "Indate=$indate Newday=$newday\n" if ($ENV{DETAIL_DEBUG} eq "1") ;
  94.                  }
  95.            return $newtime ;
  96.          }
  97. }
  98.  
  99. sub usage {
  100.     print STDERR "\nUsage : $0 <fromdate in YYYYMMDD> <todate in YYYYMMDD>\n\n( Purpose of this script is to display days between 2 given dates )\n\n" ;
  101. }
  102.  
  103. sub getYYYYMMDDforEpoch {
  104.  
  105.    my $stamp ;
  106.   ($stamp) = @_ ;
  107.   ## Given a epoch number, using localtime function, compose the date string in YYYYMMDD
  108.   return ( (localtime($stamp))[5] + 1900 ) . ( (length(( (localtime($stamp))[4] + 1 )) < 2)? "0".( (localtime($stamp))[4] + 1 ) : ( (localtime($stamp))[4] + 1 ) ) . ( ( length((localtime($stamp))[3]) < 2)? "0".(localtime($stamp))[3] : (localtime($stamp))[3] );
  109.  
  110. }
  111.  
  112. # ========================================
  113.  
Cheers
Oct 18 '07 #11
numberwhun
3,509 Expert Mod 2GB
cheguvera -

You have posted code into the forum without using proper code tags. It is best practice here on TSDN to wrap all code posted into the forum in code tags.

Code tags start with [code] and end with [/code]. If you need an example other than this one, the please refer to the REPLY GUIDELINES (or POSTING GUIDELINES if you are starting a discussion) to the right of the Message window. You will find the examples and suggestions for posting there.

In addition, you can also add the language to your code tags. Here is an example:

[code=perl]
<some code>
[/code]

Please know that I have fixed your posts above to include the proper code tags. Please be sure and use code tags in all of your future posts here on TSDN.

- Moderator
Oct 18 '07 #12
numberwhun
3,509 Expert Mod 2GB
cheguvera -

This is a learning forum and not a code writing service. We would rather a person code their solutions and learn from that than provide them code and have them learn nothing.

We have pushed him in the right direction and were waiting for an explanation of his urgency. Now, we will probably not get that. But, in return, you can now support any questions he has on the code you have provided.

Regards,

Jeff
Oct 18 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Firewalker | last post by:
Hey guys, I am newbie to perl. I am trying to deal with dates ( such trying to find what the date would be after month). Is therea function or date class( I am a java programmer, I couldnt find...
9
by: Bosconian | last post by:
I must be having a brain freeze because I can't figure out how to display dates older than 1970. I have birth dates stored in MySQL as "date" data types (exp. 1955-04-06). I'd like to display as...
2
by: sainiamit25 | last post by:
Hi All, I have two dates of the following format Wed Jul 11 18:32:46 2007 How can i compare them? I want to use comparison to run another peice of code. Thanks in advance for your help.
6
by: Dan2kx | last post by:
Good aft peeps, i have a a working solution for this already but i suppose you could say it is a bit lazy, I have used ADezii's calendar to display dates in my own unique way using the...
1
by: cmuraz | last post by:
hi, hope the query is simple, but i donno how to do. i have some records in a table for the month may 2009 as no date --------------------------------------- 1 ...
1
by: cheli198201 | last post by:
I'm totally new at this... please be patient with me. I have Access 2007 and I'm working on form where I have multiple date fields associated with each record. For example: Name: ...
2
by: Blacky | last post by:
Hi, I have a application where in on selection of Month and year, the datagrid should be display, username and the dates, the username and number of leaves from the database. UserName 1 2 3 4 5...
3
by: Tony Lucas | last post by:
When using the WHERECondition "Between And". The dates range from the 1st to 9th do not filter in to the report but if I set the date from the 31st to 10th then they do. Do you know why this...
1
by: Tracy Crehan | last post by:
I am creating a program that will calculate the monthly payment of a loan. The rate and the years are to be displayed on a hscroll bar. I have two issues that I cannot seem to resolve. 1st - I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.