Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

How to find Day of the Week from the given date (Perl)?

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-06-2008
deepakwins deepakwins is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 28 July 2016, 3:08 AM EDT
Posts: 57
Thanks: 0
Thanked 1 Time in 1 Post
How to find Day of the Week from the given date (Perl)?

How to find the Day of the Week of the given Date using perl?
If I have a date in YYY--MM-DD format, how to find the DOW? Based on that, I need to find the following sunday.
Pls help.
Sponsored Links
    #2  
Old Unix and Linux 03-06-2008
cbkihong cbkihong is offline Forum Advisor  
Advisor
 
Join Date: Sep 2002
Last Activity: 4 May 2014, 6:22 AM EDT
Location: Hong Kong, China
Posts: 1,622
Thanks: 0
Thanked 11 Times in 10 Posts
Although you may likely handle it with primitive Perl functions (such as Time::Local and localtime()), I would suggest leaving it to a module specialized for that job.

Date::Calc - Gregorian calendar date calculations - search.cpan.org
Sponsored Links
    #3  
Old Unix and Linux 03-06-2008
deepakwins deepakwins is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 28 July 2016, 3:08 AM EDT
Posts: 57
Thanks: 0
Thanked 1 Time in 1 Post
Hi,
Thanks for your reply.
I initialized

Code:
Date::Calc qw(Day_of_Week);

and used the following code in my logic,

Code:
$dow = Day_of_Week($year,$month,$day);

But iam getting follwing error
Can't locate Date/Calc.pm in @INC (@INC contains:....

Last edited by Yogesh Sawant; 07-05-2009 at 09:34 AM.. Reason: added code tags
    #4  
Old Unix and Linux 03-07-2008
deepakwins deepakwins is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 28 July 2016, 3:08 AM EDT
Posts: 57
Thanks: 0
Thanked 1 Time in 1 Post
Can any one help in this pls...
Sponsored Links
    #5  
Old Unix and Linux 03-07-2008
cbkihong cbkihong is offline Forum Advisor  
Advisor
 
Join Date: Sep 2002
Last Activity: 4 May 2014, 6:22 AM EDT
Location: Hong Kong, China
Posts: 1,622
Thanks: 0
Thanked 11 Times in 10 Posts
You have to install that module yourself as it is not shipped with Perl.

Steffen Beyer / Date-Calc-5.4 - search.cpan.org
Sponsored Links
    #6  
Old Unix and Linux 03-07-2008
deepakwins deepakwins is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 28 July 2016, 3:08 AM EDT
Posts: 57
Thanks: 0
Thanked 1 Time in 1 Post
Thanks for your effort.
Iam able to write up my own code.


Code:
my $MaxDate = '2008-03-07';

my ($MaxYear, $MaxMonth, $MaxDay, @monthDays, $YearSec, $MonthSec, $DaySec, $i);
$MaxYear = substr($MaxDate,0,4);
$MaxMonth = substr($MaxDate,5,2);
$MaxDay = substr($MaxDate,8,4);
@monthDays = (0,31,28,31,30,31,30,31,31,30,31,30,31);
(($MaxMonth = 12) && (--$MaxYear)) if (--$MaxMonth == 0);
$i=1970;
while (++$i <= $MaxYear) {
	(!($i % 400) && ($YearSec += 366)) || (!($i % 100) && ($YearSec += 365)) || (!($i % 4) && ($YearSec += 366)) || ($YearSec += 365);
}
$YearSec = $YearSec * 24 * 60 * 60;
(!($MaxYear % 400) && ($monthDays[2] = 29)) || !($MaxYear % 100) || (!($MaxYear % 4) && ($monthDays[2] = 29));
$i =0;
($MonthSec += $monthDays[$i] * 24 * 60*60) while (++$i <= $MaxMonth);
my $DaySec = ($MaxDay-1) * 24 * 60 * 60;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(18000+$YearSec+$MonthSec+$DaySec);
$year += 1900;
$mon += 1;
my $ProcessDatetime = sprintf "%4d%02d%02d%02d%02d%02d",$yedar,$mon,$mday,$hour,$min,$sec;

later found another simple method of

Code:
	my $MaxYear = substr($MaxDate,0,4);
	my $MaxMonth = substr($MaxDate,5,2);
	my $MaxDay = substr($MaxDate,8,4);
	my $weekday =  strftime("%u", 0, 0, 0, $MaxDay , $MaxMonth - 1, $MaxYear - 1900, -1, -1, -1);
	$MaxDay += 7-$weekday;
	my $sunday =  strftime("%Y%m%d", 0, 0, 0, $MaxDay , $MaxMonth - 1, $MaxYear - 1900, -1, -1, -1);

Sponsored Links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extract week start,end date from given date in PERL parthmittal2007 Shell Programming and Scripting 2 12-22-2011 10:23 PM
How to find last sunday (date) using perl? deepakwins UNIX for Dummies Questions & Answers 3 07-18-2008 04:06 PM



All times are GMT -4. The time now is 10:00 AM.