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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Function to get day of week from YYYY-MM-DD date
# 1  
Old 10-31-2009
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!!
# 2  
Old 10-31-2009
use date

Code:
echo "Enter date ([YY]YY-MM-DD) : "
read DD
# choose from one of the lines below
date --date $DD +%A # DOW complete
date --date $DD +%a # DOW abreviated
date --date $DD +%u # DOW (1..7) 1 is monday
date --date $DD +%w # DOW (0..6) 0 is sunday

To put the result in a variable, use the command expansion $( ... )
# 3  
Old 11-01-2009
Quote:
Originally Posted by frans
Code:
echo "Enter date ([YY]YY-MM-DD) : "
read DD
# choose from one of the lines below
date --date $DD +%A # DOW complete
date --date $DD +%a # DOW abreviated
date --date $DD +%u # DOW (1..7) 1 is monday
date --date $DD +%w # DOW (0..6) 0 is sunday

To put the result in a variable, use the command expansion $( ... )

That is limited to the GNU version of date, and will not work with any other version.

It can also be accomplished with the shell functions from The Dating Game.
# 4  
Old 11-02-2009
if you have perl...
Code:
YEAR=2009
MM=11
DD=02
DOW=$(perl -e 'use Time::Local;
       @abbr = qw( Sun Mon Tue Wed Thu Fri Sat );
       $DATE = timelocal(0, 0, 0, $ARGV[2], $ARGV[1]-1, $ARGV[0]-1900);
       $DAY = (localtime $DATE)[6];
       print "$abbr[$DAY]\n";' $YEAR $MM $DD)
echo Day of week is: $DOW

# 5  
Old 11-05-2009
Ta all!

Sorted!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass date (YYYY-MM-DD) as parameter and get Day

Hi, I have a requirement where I have to pass Date to a script and get the day from it. Ex If parameter is 2015-09-29 The output should be Tuesday. Can you please tell me how to get that? (6 Replies)
Discussion started by: ashwin3086
6 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. 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

5. 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

6. 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

7. Shell Programming and Scripting

Function to find day of any given date.

Hi, Is there any function in Unix by which v can find the exact day of any given date. Like i need to get Wednesday if i give 05 07 2008 (format MM DD YYYY) Thanks, RRVARMA (5 Replies)
Discussion started by: RRVARMA
5 Replies

8. HP-UX

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. (5 Replies)
Discussion started by: vpapaiya
5 Replies

9. 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

10. Programming

Function that gets the day of the week (0-6) ??

Hi , I am working at Unix system,using c lang. I need c fun which return the day of the week . For example : 0- Sunday. 1- Monday. .... 10x. (4 Replies)
Discussion started by: kamil
4 Replies
Login or Register to Ask a Question