tags:

views:

139

answers:

3

How do I get the current month as an integer, and as a string?

So for this month, I would want "7" and the string "July".

Is there an easy way to do this without a lot of string parsing and a lookup list for month names?

+6  A: 

What you need is the clock command.

http://www.tcl.tk/man/tcl8.5/TclCmd/clock.htm#M7

To get the textual representation of the month, use:

clock format [clock seconds] -format %B

And the numeric representation:

clock format [clock seconds] -format %N
jason
Actually, it's %m that gives "07" and %N gives " 7" (tcl.tk/man/tcl8.5/TclCmd/clock.htm#M52)
glenn jackman
Thanks. Corrected.
jason
"clock format" is your friend.
Michael Mathews
A: 

My company's Tcl is at version 8.4 and the %N format does not work. I guess it is a Tcl 8.5 feature. To get around that issue:

set monthNumber [string trimleft [clock format [clock seconds] -format %m] 0]
Hai Vu
+1  A: 

In tcl 8.4 you can use %h and it will return the abbreviated month name ( eg. Oct )

John