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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to obtain a day of the week from the date?
# 1  
Old 11-28-2012
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 to post them!!!

Last edited by baranisachin; 11-28-2012 at 11:16 AM..
# 2  
Old 11-28-2012
Code:
#!/bin/ksh

DATE="yyyymmdd"

echo "${DATE:6:2}"

# 3  
Old 11-28-2012
This is one way in bash shell:
Code:
date -d2012-11-28 +'%A %u'

# 4  
Old 11-28-2012
spacebar, this is not a 'bash feature'. date is an external utility, not part of bash -- your code would work equally well in a variety of shells, but has one problem. The -d switch is a nonstandard GNU feature, something vanilla AIX does not have.

Running the date external utility to extract two characters from a string is overkill of the nth order, in any case.
# 5  
Old 11-28-2012
Code:
perl -MPOSIX=mktime -e '@wday=qw(Sun Mon Tue Wed Thu Fri Sat);
$ARGV[0] =~ /(....)(..)(..)/;
print $wday[(localtime mktime(0,0,0,$3,$2-1,$1-1900))[6]],"\n"' YYYYMMDD

Replace YYYYMMDD with your date.
# 6  
Old 11-28-2012
Hi elixir... can u pls explain the solution?

---------- Post updated at 11:14 AM ---------- Previous update was at 11:10 AM ----------

Hi spacebar... Theres s no -d option available in AIX flavour
# 7  
Old 11-28-2012
That's using the mktime subroutine from the POSIX core perl module (check perldoc POSIX for more details) to convert the given time to seconds since the Epoch. Then, the localtime builtin function (see perldoc perlfunc for the details) is used to convert that value to a list. In this list, the 7th value (index 6) is the weekday. Then, this weekday value is used as an index to get the required weekday name from the defined list.
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 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

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

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

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

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

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. Shell Programming and Scripting

Yesterday's Day of week

I need o get yesterday's day of week but im not exactly sure. the actual name is what i want. I can do it with numbers but im not sure with words. (3 Replies)
Discussion started by: rcunn87
3 Replies
Login or Register to Ask a Question