Finding Day of the week from date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding Day of the week from date
# 1  
Old 10-07-2010
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
# 2  
Old 10-07-2010
nawk, not awk

Go here: AWK Language Programming - A Library of awk Functions
Get the code for the mktime() function, which returns epoch seconds from a date. Note: read the article, you need to get several functions, not just one.
The number 0 .. 6 of the day of the week is
Code:
dow= (epochseconds / 86400 ) % 7

If you need day names, then create an array and access each element by dow,
day_name[dow]
# 3  
Old 10-07-2010
You can give this a try:
Code:
#!/bin/sh

# Day of the week (YYYYMMDD)

d="20101007"

day=${d#??????}
temp=${d#????}
month=${temp%??}
year=${d%????}

DOW=$(cal $month $year | awk '
BEGIN{split("Sun Mon Tue Wed Thu Fri Sat",dow);dow[0]=dow[7]}
NR==3{t=7-NF+d;print dow[t%7];exit}' d=$day)

echo $DOW

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 4  
Old 10-07-2010
If it is current date then you can use :

date +"%A"
# 5  
Old 10-07-2010
Check out the gnu date command from their corutils ,

Examples of date - GNU Coreutils

You can do this:
$ date -d 20101007 "+%A"
Thursday


You can get the output for whatever date you put in after the -d

Last edited by ctafret; 10-07-2010 at 03:13 PM..
# 6  
Old 10-07-2010
FreeBSD
Code:
# date -j -f "%Y%m%d" "20101007" "+%A"
Thursday

or just
Code:
# date -d 20101007 "+%A"
Thursday

# 7  
Old 10-11-2010
Thanks all for you replies:

On my solaris machine date -d doesn't work.
Niether does date -j -f...Smilie(

Franklin52: I have to do this process (dow) for every record in a file, besides other fuctions that are to be performed on other fields of data.

I have written a piece of code as:

Code:
awk 'BEGIN{
	FS="|";
	while(getline < "DAY_MAPPING_NEW.CFG")
	{
	DAY_NEW[$2]=$1; DAY_NEW[$3]=$1; DAY_NEW[$4]=$1; DAY_NEW[$5]=$1; DAY_NEW[$6]=$1; DAY_NEW[$7]=$1;
	}
}
{
	dd=substr("'${DATE}'",7,2);
	mm=substr("'${DATE}'",5,2);
	yyyy=substr("'${DATE}'",1,4);
	system("cal "mm" "yyyy" | tail -7|tr \" \" \";\"|tr \"\\n\" \";\" >out.txt");
	while(getline < "out.txt")
	{OPFILE=$0;}
	if (int(dd) <= 9) {	dd=substr("'${DATE}'",8,1);	}
	newdd=";"dd";"
	daynum=index(OPFILE,newdd);
	print "New Date is:"dd"|"mm"|"yyyy"|"DAY_NEW[daynum];
	system("rm out.txt");


	OTHER FUNCTIONALITIES
		.
		.
		.
		.
		..
		..
		.
		.
		..
}' INPUTFILE.txt

But i want to get output of system commant in some variable rather than writting o/p in a file "out.txt' and removing it everytime.

Last edited by Scott; 10-11-2010 at 06:58 AM.. Reason: Code tags, please...
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. 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 the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 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 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. UNIX for Dummies Questions & Answers

Changing First Day Of The Week?

Hi All, Our system is running on Solaris 8 and we are using US locale. By default the First Day Of Week is Sunday, is it possible for us to change it to Monday? I have googled it but found very little of use. THanks in advance. (2 Replies)
Discussion started by: fowlerleftfoot
2 Replies
Login or Register to Ask a Question