The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How create function valid birthday format dd-mm-yyyy kency Shell Programming and Scripting 3 10-14-2009 02:01 PM
converting the date field from dd/mm/yyyy to yyyy/mm/dd pareshan Shell Programming and Scripting 4 03-17-2009 05:43 PM
Change Date from dd-mmm-yyyy to mm/dd/yyyy stringzz Shell Programming and Scripting 9 09-15-2008 10:09 AM
Date Vadidation in YYYY-MM-DD itsmehihihi Shell Programming and Scripting 1 07-22-2007 09:22 PM
sed to display date in dd/mm/yyyy format sars Shell Programming and Scripting 2 02-15-2007 03:32 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 3 Weeks Ago
couponmeup couponmeup is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 2
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 (permalink)  
Old 3 Weeks Ago
frans's Avatar
frans frans is offline
Registered User
  
 

Join Date: Oct 2009
Location: Drôme, France
Posts: 86
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 (permalink)  
Old 2 Weeks Ago
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by frans View Post
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 (permalink)  
Old 2 Weeks Ago
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
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 (permalink)  
Old 2 Weeks Ago
couponmeup couponmeup is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 2
Ta all!

Sorted!
Reply

Bookmarks

Tags
convert, date, perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0