Solved Hi I need the result in YYYYMMDD fornat

August 5, 2011 at 12:06:41
Specs: Windows XP
Hi Nails, please can u help again..I need the result from get_greg_from_JD() in YYYYMMDD format unlike YYYYMD

See More: Hi I need the result in YYYYMMDD fornat

Report •

#1
August 5, 2011 at 14:07:01
I imagine you are asking about get_greg_from_JD from this thread:

http://www.computing.net/answers/un...

I won't change the format of the get_greg_from_JD function, but you can do something like this if you are running ksh or bash: typeset the month and day variable 2 characters long. with -Z. blanks will be zero filled:

.
.
yesterdays_date_str=$(get_greg_from_JD $((JD-1)) )

# parse yesterdays date string
set - $(echo $yesterdays_date_str)
echo $1 # month
echo $2 # day
echo $3 # year

typeset -2Z mm
typeset -2Z dd
mm=$1
dd=$2
year=$3

datestr="${year}${mm}${dd}"
echo $datestr


Report •

#2
August 8, 2011 at 08:49:55
Hi Nails, Thanks, but Its not helping...The result for my get_greg_from_JD() is 201232, and when I do this parsing it gives 32 as $datestr, where as I want the result as 20120302..Please help.


Report •

#3
August 8, 2011 at 10:28:08
✔ Best Answer
I don't understand your reply. The original get_greg_from_JD function returned a date string in the format:

Month Day Year

I will repost the original korn shell script below. This script gets yesterday's date in the format:

YYYYMMDD

<cut here>

#!/bin/ksh

# The Julian date (JD) is a continuous count of days from 1 January 4713 BC.
# The following # algorithm is good from years 1801 to 2099
# See URL: http://aa.usno.navy.mil/faq/docs/JD... for more information
get_JD ()
{
typeset -i JDD

JDD=$(($1-32075+1461*($3+4800+($2-14)/12)/4+367*($2-2-($2-14)/12*12)/12-3*(($3+4900+($2-14)/12)/100)/4))
echo $JDD
}

# This function computes the gregorian date from the julian date - $1. Returns a
# date strin of the form: MONTH DAY YEAR
# See URL: http://aa.usno.navy.mil/faq/docs/JD... for more information
get_greg_from_JD ()
{
typeset -i L
typeset -i N
typeset -i I
typeset -i J
typeset -i DAY
typeset -i MON
typeset -i YR

L=$(($1+68569)) # $1 is the julian date
N=$((4*L/146097))
L=$((L-(146097*N+3)/4))
I=$((4000*(L+1)/1461001))
L=$((L-1461*I/4+31))
J=$((80*L/2447))
DAY=$((L-2447*J/80))
L=$((J/11))
MON=$((J+2-12*L))
YR=$((100*(N-49)+I+L))

echo $MON $DAY $YR
}

TY=$(date '+%Y') # Year for today
TM=$(date '+%m') # Month for today
TD=$(date '+%d') # Day for today
# printf "%s %s %s\n" $TM $TD $TY
JD=$(get_JD TD TM TY) # today's Julian date

# yesterday's date
yesterdays_date_str=$(get_greg_from_JD $((JD-1)) )

# parse yesterdays date string
set - $(echo $yesterdays_date_str)
echo $1 # month
echo $2 # day
echo $3 # year

typeset -2Z mm
typeset -2Z dd
mm=$1
dd=$2
year=$3

datestr="${year}${mm}${dd}"
echo $datestr


Report •

Related Solutions

#4
August 8, 2011 at 11:47:26
Thnaks..It works perfect! I was doing something wrong somewhere...

Report •

Ask Question