Tony Davis
01-07-2004, 04:37 PM
Is there any way of determining what day of the week a certain date is? For example, if I am given a date of January 15th, 2004, is there a method of determining what day of the week that is within the Perl script?
Thanks,
Tony Davis
YUPAPA
01-08-2004, 05:50 AM
You mean weekdays like Monday, Tuesday... ?
#!/usr/bin/perl
use strict;
my ($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my @weekdays = ('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
print "$weekdays[$wday]\n";
mlseim
01-08-2004, 02:47 PM
I find it easiest to do everything in "Julian Dates" ...
Below is a test script to experiment with a couple of
subroutines to do the conversions.
#! /usr/bin/perl
# ---------------------------------------
# the text used for the names of the months
@monthnames=("January","February","March","April",
"May","June","July","August","September",
"October","November","December");
@shortmonthnames=("Jan","Feb","Mar","Apr",
"May","Jun","Jul","Aug","Sep",
"Oct","Nov","Dec");
# array for the days of the week.
@daynames=(" ","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
@shortdaynames=(" ","S","M","T","W","T","F","S");
# Example of getting the Julian Date for TODAY:
$jtoday = juliantoday();
# Example of finding the day of week for the following date:
$year = '2004';
$month='11';
$day='19';
# This would be the actual Julian Date ($jdate)
$jdate=tojulian($year, $month, $day);
# This would be the day of week number.
# Sunday is actually day 0 (zero) Sun=0, Mon=1, Tue=2 ... Sat=6
$dayofweek=($jdate+1)%7;
print "Content-type: text/html\n\n";
print "
<br>
Example:<br><br>
The Julian Today is: $jtoday<br><br>
The Julian Date for $monthnames[$month-1] $day, $year is: $jdate<br><br>
The day of week is: $daynames[$dayofweek+1]<br><br>
";
##############################################################
# Convert from Date to Julian Date
##############################################################
# converts year, month, day to Julian Date
# ------------------------
# input: year, month, day
# output: julian_date
#
sub tojulian {
use integer;
my($year, $month, $day) = @_;
return $day - 32075
+ 1461 * ( $year + 4800 - ( 14 - $month ) / 12 ) / 4
+ 367 * ( $month - 2 + ( ( 14 - $month ) / 12 ) * 12 ) / 12
- 3 * ( ( $year + 4900 - ( 14 - $month ) / 12 ) / 100 ) / 4;
}
##############################################################
# Convert from Julian Date to Date
##############################################################
# converts julian day to year, month and day
# ------------------------------------------
# input: julian_date
# output: year, month, day
#
sub fromjulian {
use integer;
my($jd) = @_;
my($jdate_tmp,$m,$d,$y);
$jdate_tmp = $jd - 1721119;
$y = (4 * $jdate_tmp - 1) / 146097;
$jdate_tmp = 4 * $jdate_tmp - 1 - 146097 * $y;
$d = $jdate_tmp/4;
$jdate_tmp = (4 * $d + 3)/1461;
$d = 4 * $d + 3 - 1461 * $jdate_tmp;
$d = ($d + 4)/4;
$m = (5 * $d - 3) / 153;
$d = 5 * $d - 3 - 153 * $m;
$d = ($d + 5) / 5;
$y = 100 * $y + $jdate_tmp;
if ($m < 10) {
$m += 3;
} else {
$m -= 9;
++$y;
}
return ($y, $m, $d);
}
##############################################################
# Get Today's Julian Date
##############################################################
# returns today's julian date
# ---------------------------
# output: julian_date
#
sub juliantoday {
# current date
my($sec,$min,$hour,$day,$month,$year,$wday,$yday,$isdst) =
localtime(time);
$year+=($year < 70) ? 2000 : 1900;
return tojulian($year, $month + 1, $day);
}
Tony Davis
01-09-2004, 07:29 PM
I'll give it a shot.
Thanks!
mlseim
01-13-2004, 04:28 AM
Also,
You can use Julian dates to do some math, such as
the difference of days between dates.
Go to this URL and enter your birthday ...
http://www.catpin.com/cgi-bin/julian.pl
(see how many DAYS old you are!)
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.