Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Day of Year 1

Status
Not open for further replies.

cmhunt

Programmer
Apr 17, 2001
119
GB
Hi

We are currently obaining the day of the year with
date +'%j'
so that it will return 016 for today (16/1/2003). How cam we return the number without any leading zeros (ie 16 for today). I know this is probably very easy but I don't know UNIX scripting very well and I am trying to fix existing code.

Thanks

Chris
 
Hi Chris,
type in your script
typeset -i dt=`date +%j`
Regards Boris
 
I would have used date +%j|awk '{printf("%d",$0)}' ... but like Boris' solution better :)

Greg.
 
Hi guys. Thanks for your help. I can't get Boris' solution to work (tells me that Typeset: Command to found) but Greg's does the job.

Thanks again!

Chris
 
The [tt]typeset[/tt] command is Korn shell. You must be using a different shell.
 
try this also,
but your question is wrong. %j doenot give you day of the year.but anyway it is giving something with trailing zero

ans: date +'%j'|cut -c 2,3

or

date +'%d' which gives the date.

Regards
Santosh.K.B

 
For ksh:
typeset -i dt=$(date +%j)
For bash:
declare -i dt=$(date +%j)
For sh:
date +%j | awk '{print -(-$0)}'
date +%j | sed -e 's/^00*//'

And so on, and so on :)
 
The date command has built-in optional field width and precision specifications. If no field width or precision is specified for the j directive, the default is .3

So date +'%j' defaults to date +'%.3j' but the command that you actually want is date +'%.1j'
 
I have not been successful running the command Ygor noted ...
date +'%j' = some number, todays year day
date +'%.3j' = %.3j

what am I missing?

Ygor (Programmer) Apr 10, 2003
The date command has built-in optional field width and precision specifications. If no field width or precision is specified for the j directive, the default is .3

So date +'%j' defaults to date +'%.3j' but the command that you actually want is date +'%.1j'
 
I think that "%.1j" date format is for a specific version of the date command (for example, the Linux version). I've not heard of the standard date command does supporting this.
 
You could simply say:

if (( $DAY_OF_YR < 100 )) then
DAY_OF_YR=$DAY_OF_YR|cut -c2-3
if (( $DAY_OF_YR < 10 )) then
DAY_OF_YR=$DAY_OF_YR|cut -c3
fi
fi

It's a bit of a phaffy way around it, but it (or something like it) will work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top