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!

How to display century in UNIX.

Status
Not open for further replies.

Queenyunix

Programmer
Aug 29, 2011
7
HK
Hi,

I wanted to display Century in Unix.

I tried with date +"%C"

Which is displaying Mon Aug 29 12:54:54 BST 2011. (Used capital C).
and when used date +"%c"

Displaying Mon Aug 29 12:54:33 2011. (used Small c)

But i want century (21) to get displayed. Please let me know how can i do that.
 
Well, capital %C should work in some flavours of Unix. But you haven't said which flavour you're using? What's the output of uname -a?

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
hi, Thanks for the reply..

Version is

SunOS bfu226a002 5.9 Generic_118558-13 sun4u sparc SUNW,Sun-Fire-V240

Please suggest what can be done.
 
I think you might be falling between two standards there; try /usr/xpg4/bin/date +%C perhaps?

Unfortunately I don't have access to a Solaris box right now to try it.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Alternatively, date +%Y | cut -c 1-2.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Hi both the commands are working.
but its giving 20. I need century 21 to be displayed.
Thank you.
 
Actually i wanted to display the complete date along with century like 21110830.

For this i used date +%C%y%m%d

But not working.. :(
 
Can I perhaps ask why this is needed?

You could adapt Annihilanic's solution and just add 1 to the output if it's absolutely necessary?

The internet - allowing those who don't know what they're talking about to have their say.
 
Because i need to send a file with name given as 21110830.

Please suggest me what else can be done?
 
I have used the command
echo filename`echo $(/usr/xpg4/bin/date +%C) +1 |bc``date "+%y%m%d"`.csv
i am getting filename21110830.csv as required, but the same command when used in a shell script, iam getting error saying
syntax error at line 1: `(' unexpected

Please let me know,what could be the reason?
 
Hi,

got my result by giving

filename"`/usr/xpg4/bin/date +'%C + 1' | bc`"`date "+%y%m%d"`.csv


Thank you all.. :)
 
I think you might want to double check your requirements with whoever gave you the specs. When someone asks for the year, including the "century", they are usually looking for "2011", not "2111". Asking for the year, including century, just means they want all four digits of the year, not the four digit year plus 100 years. That makes no sense.

If it does need to be the century as you are requesting, here's some simpler code that does the same thing.
Code:
filename21`date "+%y%m%d"`.csv
Yes, this code will break in less than 89 years, but I bet you a dollar it won't still be used by then. [bigsmile]

 
Good point well made, Sam. Like you, I couldn't for the life of me figure out why anyone would spec a filename that way.

Still, takes all sorts and glad Queenyunix got a solution!

The internet - allowing those who don't know what they're talking about to have their say.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top