Sponsored Content
Top Forums Shell Programming and Scripting calcuate the week number of a given date Post 83039 by Perderabo on Friday 9th of September 2005 01:15:14 PM
Old 09-09-2005
Thanks for posting your final solution. I don't recall ever hearing of ISO week numbers before. So I will add this thread to our date arithmetic FAQ article. I found this page which has an on-line calculator for ISO week numbers.

For the record, here is a datecalc based solution. It emulates the behavior of the on-line calculator in that it outputs a year, a week number, and a day of week number...
Code:
#! /usr/bin/ksh
month=$1
day=$2
year=$3
typeset -Z2 fwk

function calcisoweek
{
        integer year month day dow1y mjd1y mjd isoweek
        year=$1
        month=$2
        day=$3
        dow1y=$(datecalc -d $year 1 1)
        ((dow1y=(dow1y+6)%7))
        mjd1y=$(datecalc -j $year 1 1)
        mjd=$(datecalc -j $year $month $day)
        ((isoweek=(mjd - mjd1y - dow1y + 7 + 3 )/7))
        echo $isoweek
        return 0
}

dow=$(datecalc -d $year $month $day)
((!dow)) && dow=7
fwk=$(calcisoweek $year $month $day)
if ((!fwk)) ; then
        ((year=year-1))
        fwk=$(calcisoweek $year 12 31)
fi
echo ${year}-W${fwk}-${dow}
exit 0

 

7 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

3. Shell Programming and Scripting

Know the number of the week for a date

Hi, I tried to find the solution on the forum without success. datecalc from Perderabo doesn't solve my problem. I would like to know how to do the same thing that date +%U but for a specific date. For example: 2011 08 27 => 39 Thinks a lot (8 Replies)
Discussion started by: Castelior
8 Replies

4. Shell Programming and Scripting

Extract week start,end date from given date in PERL

Hi All, what i want to do in perl is i should give the date at run time .Suppose date given is 23/12/2011(mm/dd/yyyy) the perl script shold find week start date, week end date, previous week start date,end date,next week start date, end date. In this case week start date will be-:12/19/2011... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

5. Shell Programming and Scripting

Week number from a date.

Hi, How can we get the week number from any paricular date. lets say date is 20120404 (YYYYMMDD) then how to get the week number? date +%W --- Thic command gives the week number for current date only. Thanks. (13 Replies)
Discussion started by: 46019
13 Replies

6. AIX

How to calcuate total number of weeks?

Hi anyone can help? How to calculate total number of weeks from a specify date, for example, 01 Jan 2012. Thx! https://www.unix.com/images/misc/progress.gif (1 Reply)
Discussion started by: rayray2013
1 Replies

7. Shell Programming and Scripting

Find week of the year for given date using date command inside awk

Hi all, Need an urgent help on the below scenario. script: awk -F"," 'BEGIN { #some variable assignment} { #some calculation and put values in array} END { year=#getting it from array and assume this will be 2014 month=#getting it from array and this will be 05 date=#... (7 Replies)
Discussion started by: vijaidhas
7 Replies
GREGORIANTOJD(3)							 1							  GREGORIANTOJD(3)

gregoriantojd - Converts a Gregorian date to Julian Day Count

SYNOPSIS
int gregoriantojd (int $month, int $day, int $year) DESCRIPTION
Valid Range for Gregorian Calendar 4714 B.C. to 9999 A.D. Although this function can handle dates all the way back to 4714 B.C., such use may not be meaningful. The Gregorian calendar was not instituted until October 15, 1582 (or October 5, 1582 in the Julian calendar). Some countries did not accept it until much later. For exam- ple, Britain converted in 1752, The USSR in 1918 and Greece in 1923. Most European countries used the Julian calendar prior to the Grego- rian. PARAMETERS
o $month - The month as a number from 1 (for January) to 12 (for December) o $day - The day as a number from 1 to 31 o $year - The year as a number between -4714 and 9999 RETURN VALUES
The julian day for the given gregorian date as an integer. EXAMPLES
Example #1 Calendar functions <?php $jd = GregorianToJD(10, 11, 1970); echo "$jd "; $gregorian = JDToGregorian($jd); echo "$gregorian "; ?> SEE ALSO
jdtogregorian(3), cal_to_jd(3). PHP Documentation Group GREGORIANTOJD(3)
All times are GMT -4. The time now is 07:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy