Get Day of Week from date


 
Thread Tools Search this Thread
Operating Systems HP-UX Get Day of Week from date
# 1  
Old 08-23-2005
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.
# 2  
Old 08-23-2005
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..
# 3  
Old 08-23-2005
Awesome !!!

Appreciate vgersh99
# 4  
Old 03-06-2008
Can the same logic be rewriten into perl?
# 5  
Old 03-07-2008
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  
Old 03-07-2008
Thanks Jim, Its helps!!!Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get the week's day

Hi All, I have the below requirement , if i give the week number for ex 41 i need to get the date for Monday and thursday for this given week. my expected output is 13/10/2014 (Monday's date) and 16/10/2014 (Thursday's date) I am using GNU LINUX . Pls help me with your thoughts. Thanks in... (7 Replies)
Discussion started by: mohanalakshmi
7 Replies

2. Shell Programming and Scripting

How to get the consecutive last 10 week day date using UNIX ksh shell scripting?

Hi, i am writing a ksh shell script to check the last month end date whether it is falling in last 10 week day date, I am not sure How to use "Mr. Perderabo's date calculator", Could you Please let me know how to use to get my requirement, I tried my own script but duplicate week day and... (5 Replies)
Discussion started by: karthikram
5 Replies

3. Shell Programming and Scripting

How to obtain a day of the week from the date?

I have a date in format YYYYMMDD, i need to get the day of the week from the given date. I am working in AIX system. ---------- Post updated at 09:59 AM ---------- Previous update was at 09:57 AM ---------- Tried to post sum of the thread's link from which i tried, but de rules didnt allow me... (9 Replies)
Discussion started by: baranisachin
9 Replies

4. HP-UX

Find Day of Week

In HP-UX the date command does not have the "-d" switch like some other *nixes do. I'm working a simple script to tell me, given the day, month and year what day of the week that falls on. Assuming valid day, month and year input (I'd perform quality checks on the input separately, but not... (5 Replies)
Discussion started by: rwuerth
5 Replies

5. Shell Programming and Scripting

How to add day of week at the end of each line that shows the date?

I have a file that looks like: file1: www_blank_com 20121008153552 www_blank_com 20121008162542 www_blank_com 20121009040540 www_blank_com 20121009041542 www_blank_com 20121010113548 www_blank_com 20121011113551 www_blank_com 20121012113542 I want the new file to show the day of... (3 Replies)
Discussion started by: castrojc
3 Replies

6. Shell Programming and Scripting

Finding Day of the week from date

I have a problem of Finding Day of the week from date, but i need to do it within awk On SOLARIS Input:20101007(YYYYMMDD) Output:Thursday kindly provide suggestions. Thanks in advance (8 Replies)
Discussion started by: junaid.nehvi
8 Replies

7. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

8. UNIX for Dummies Questions & Answers

Day of the week from a string

Hi All, I need to know how to derive the day of the week by passing the value in following format: Feb 28 2010 The output I'm expecting is Sunday or Sun. I know, I can use the following code to get the day of the week. date +%a But I want to pass the value as a string. Please help... (11 Replies)
Discussion started by: shash
11 Replies

9. Shell Programming and Scripting

Function to get day of week from YYYY-MM-DD date

Can't find out how to get the day of the week from a given date, anyone got a code snippet that could help please? Ta!! (4 Replies)
Discussion started by: couponmeup
4 Replies

10. UNIX for Dummies Questions & Answers

How to find Day of the Week from the given date (Perl)?

How to find the Day of the Week of the given Date using perl? If I have a date in YYY--MM-DD format, how to find the DOW? Based on that, I need to find the following sunday. Pls help. (5 Replies)
Discussion started by: deepakwins
5 Replies
Login or Register to Ask a Question