The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > OS Specific Forums > HP-UX
Google UNIX.COM


HP-UX HP-UX (Hewlett Packard UniX) is Hewlett-Packard's proprietary implementation of the Unix operating system, based on System V.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 08-23-2005
Registered User
 

Join Date: Aug 2005
Posts: 2
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.
Reply With Quote
Forum Sponsor
  #2  
Old 08-23-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
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.
Reply With Quote
  #3  
Old 08-23-2005
Registered User
 

Join Date: Aug 2005
Posts: 2
Awesome !!!

Appreciate vgersh99
Reply With Quote
  #4  
Old 03-06-2008
Registered User
 

Join Date: Feb 2007
Posts: 38
Can the same logic be rewriten into perl?
Reply With Quote
  #5  
Old 03-07-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,269
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")"
Reply With Quote
  #6  
Old 03-07-2008
Registered User
 

Join Date: Feb 2007
Posts: 38
Thanks Jim, Its helps!!!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0