|
|||||||||
| HP-UX HP-UX (Hewlett Packard UniX) is Hewlett-Packard's proprietary implementation of the Unix operating system, based on System V. |
linux operating commands and unix operating commands |
|
|
|
Thread Tools | Search this Thread | 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. |
| Sponsored Links | ||
|
|
#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 04:59 PM.. |
| Sponsored Links | ||
|
|
|
#3
|
||||
|
||||
|
Awesome !!!
Appreciate vgersh99
|
|
#4
|
||||
|
||||
|
Can the same logic be rewriten into perl?
|
| Sponsored Links | |
|
|
#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")" |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
Thanks Jim, Its helps!!!
|
| Sponsored Links | ||
|
|
![]() |
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 11:23 PM |
| how to obtain date and day of the week from `date` command | aoussenko | Shell Programming and Scripting | 1 | 06-28-2010 12:15 PM |
|
|