![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| HP-UX HP-UX (Hewlett Packard UniX) is Hewlett-Packard's proprietary implementation of the Unix operating system, based on System V. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to find Day of the Week from the given date (Perl)? | deepakwins | UNIX for Dummies Questions & Answers | 5 | 03-07-2008 01:45 PM |
| get date of Thursday of any week | Miller | UNIX for Advanced & Expert Users | 2 | 10-21-2007 08:47 PM |
| check file date in one week ??? | sabercats | Shell Programming and Scripting | 1 | 03-16-2006 05:43 AM |
| How to identify week-1 and week-2 | csaha | Shell Programming and Scripting | 1 | 02-16-2006 02:24 AM |
| calcuate the week number of a given date | ahmedwaseem2000 | Shell Programming and Scripting | 11 | 09-11-2005 09:41 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Get Day of Week from date
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. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Code:
#!/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
}
}
'
Last edited by vgersh99; 08-23-2005 at 12:59 PM. |
|
#3
|
|||
|
|||
|
Awesome !!!
Appreciate vgersh99
|
|
#4
|
|||
|
|||
|
Can the same logic be rewriten into perl?
|
|
#5
|
|||
|
|||
|
Change the variable $fmt to get weekday in the format you want I added a comment there:
try - Code:
#!/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")"
|
|
#6
|
|||
|
|||
|
Thanks Jim, Its helps!!!
|
|||
| Google The UNIX and Linux Forums |