User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 363,779 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,548 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser:
Oct 12th, 2006
Views: 5,116
Given the date in mm-dd-yyyy (or M-D-YYYY) format (or any combination of that format) you can find out what day of the week that was on or is on or will be on, within the limitations of the system the script resides on.

This snippet uses only core functions and modules so nothing needs to be installed but it is worth mentioning that there are numerous date modules on CPAN that offer a wide range of functions for handling date calculations.

http://search.cpan.org/search?query=date&mode=all

This snippet assumes the date is in mm-dd-yyyy format but can easily be adapted to handle any combination of those date parameters. Can also handle years in three digit and two digit format but see the Time::Local and localtime() documentation for limitations.
Last edited : Oct 13th, 2006.
perl Syntax | 4 stars
  1. use Time::Local 'timelocal_nocheck';
  2.  
  3. # An array to hold the names of each day of the week.
  4. my @weekday = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
  5.  
  6. # '09-11-2001' can be a parameter/argument you pass to the script
  7. my $mm_dd_yyyy = '09-11-2001';
  8. my $day_of_week = get_day($mm_dd_yyyy);
  9. print "$mm_dd_yyyy was a $day_of_week";
  10.  
  11. sub get_day {
  12. my $date = shift || return(0);
  13. my ($mon,$mday,$year) = $date =~ /(\d+)-(\d+)-(\d+)/;
  14. my $epochtime = timelocal_nocheck(0, 0, 0, $mday, $mon-1, $year);
  15. my $day = (localtime($epochtime))[6];
  16. return $weekday[$day];
  17. }
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 10:39 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC