Hi All,
I have date in string format 'YYYY-MM-DD'. I want to know day of the week for this date.
Example. For '2005-08-21' my script should return '0' or Sunday
For '2005-08-22' it should return '1' or Monday
I want piece of code for HP-UX korn shell.
Appreciate reply on this.
#!/bin/ksh
date='2005-08-30'
eval $(echo "${date}" | nawk -F- '{printf("year=%s month=%s day=%s\n", $1, $2, $3)}')
echo "year->[${year}] month->[${month}] day->[${day}]"
cal "${month}" "${year}" | nawk -v day="${day}" '
FNR > 2 {
for(i=1; i <= NF; i++)
if ( $i == day) {
#printf("day->[%d] row->[%d]\n", $i, FNR-2)
printf("%d\n", (NF == 7 || FNR!=3) ? i-1 : i+(6-NF))
exit
}
}
'
Can the same logic be rewriten into perl?
Change the variable $fmt to get weekday in the format you want I added a comment there:
try -
#!/bin/ksh
# input format YYYY-MM-DD prints day name
dow()
{
perl -e '
use POSIX qw(strftime);
$fmt = "%A"; # %a = abbreviated weekday %u = weekday number
$mday = substr("$ARGV[0]", 8, 2);
$mon = substr("$ARGV[0]", 5 ,2);
$year = substr("$ARGV[0]", 0 ,4);
$weekday =
strftime($fmt, 0, 0, 0, $mday , $mon - 1, $year - 1900, -1, -1, -1);
print "$weekday";
' "$1"
}
echo "today is $(date)"
echo "$(dow `date "+%Y-%m-%d"` )"
echo "2007-10-03 was a $(dow "2007-10-03")"