Unix/Linux Go Back    


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

Get Day of Week from date

HP-UX


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-23-2005   -   Original Discussion by vpapaiya
vpapaiya's Unix or Linux Image
vpapaiya vpapaiya is offline
Registered User
 
Join Date: Aug 2005
Last Activity: 23 August 2005, 4:21 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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  
Old Unix and Linux 08-23-2005   -   Original Discussion by vpapaiya
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 13 January 2018, 6:50 AM EST
Location: Foxborough, MA
Posts: 8,204
Thanks: 231
Thanked 786 Times in 733 Posts


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  
Old Unix and Linux 08-23-2005   -   Original Discussion by vpapaiya
vpapaiya's Unix or Linux Image
vpapaiya vpapaiya is offline
Registered User
 
Join Date: Aug 2005
Last Activity: 23 August 2005, 4:21 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Awesome !!!

Appreciate vgersh99
    #4  
Old Unix and Linux 03-06-2008   -   Original Discussion by vpapaiya
deepakwins's Unix or Linux Image
deepakwins deepakwins is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 13 December 2017, 3:39 PM EST
Posts: 62
Thanks: 0
Thanked 1 Time in 1 Post
Can the same logic be rewriten into perl?
Sponsored Links
    #5  
Old Unix and Linux 03-07-2008   -   Original Discussion by vpapaiya
jim mcnamara's Unix or Linux Image
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 12 January 2018, 4:18 PM EST
Location: NM
Posts: 11,280
Thanks: 581
Thanked 1,125 Times in 1,036 Posts
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  
Old Unix and Linux 03-07-2008   -   Original Discussion by vpapaiya
deepakwins's Unix or Linux Image
deepakwins deepakwins is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 13 December 2017, 3:39 PM EST
Posts: 62
Thanks: 0
Thanked 1 Time in 1 Post
Thanks Jim, Its helps!!!Linux
Sponsored Links
Closed


Linux 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



All times are GMT -4. The time now is 09:26 AM.