You can use the Tcl command clock seconds to return the current time and clock format to generate a custom-formatted date time string.
The clock format command accepts an optional argument named -format that specifies a string that controls how the date time string is formatted. You can specify date and time keywords that get replaced with specific parts of the current date and time.
Here's an example of the command that generates a date time string with the following elements, in this order:
- Four digit year
- Two digit month (01-12)
- Two digit day (01-31)
- Two digit hour in 24-hour format (00-23)
- Two digit minute (00-59)
- Two digit seconds (00-59)
clock format [clock seconds] -format {%Y %m %d %H %M %S}
That command generates this string:
2005 01 10 15 16 55
Here is a table of formatting strings you can use with the clock format command.
%% | The % sign |
---|---|
%a | Abbreviated weekday name (Mon, Tue, etc.) |
%A | Full weekday name (Monday, Tuesday, etc.) |
%b | Abbreviated month name (Jan, Feb, etc.) |
%B | Full month name |
%c | Locale specific date and time |
%d | Day of month (01 - 31) |
%H | Hour in 24-hour format (00 - 23) |
%I | Hour in 12-hour format (00 - 12) |
%j | Day of year (001 - 366) |
%m | Month number (01 - 12) |
%M | Minute (00 - 59) |
%p | AM/PM indicator |
%S | Seconds (00 - 59) |
%U | Week of year (00 - 52), Sunday is the first day of the week |
%w | Weekday number (Sunday = 0) |
%W | Week of year (00 - 52), Monday is the first day of the week |
%x | Locale specific date format |
%X | Locale specific time format |
%y | Year without century (00 - 99) |
%Y | Year with century (e.g. 1990) |
%Z | Time zone name |
Examples
Here are other examples of the output for different format strings.
Example 1
clock format [clock seconds] -format {%b. %d, %Y}
Jan. 10, 2005
Example 2
clock format [clock seconds] -format {%I:%M:%S %p}
03:21:01 PM