Home > Software engineering >  How to get date in DDMMYYYY format in TCL?
How to get date in DDMMYYYY format in TCL?

Time:08-24

I am creating a TCL script which creates a log file. The time and date are made part of the log file name.

This is what I have at present:

  set fname_date [clock format [clock seconds] -format {%d-%h-%y}]
  set fname_time [clock format [clock seconds] -format {%H%M%S}]

  set    log_fname "test_log"
  append log_fname "_$fname_time"
  append log_fname "_$fname_date"
  append log_fname ".dat"

This gives the filename as:

test_log_155838_23-Aug-22.dat

However, I need the date to be all numbers so the filename looks like this instead:

life_test_log_155838_230822.dat

How to get the current date in DDMMYY format from TCL? I am not sure what format string to use to do this.

CodePudding user response:

The clock format details are here: https://www.tcl.tk/man/tcl8.4/TclCmd/clock.html

DDMMYY format is %d%m%y

DDMMYYYY format is %d%m%Y

CodePudding user response:

Long form, your code adapted

set fname_date [clock format [clock seconds] -format %d%m%y]
set fname_time [clock format [clock seconds] -format %H%M%S]

set    log_fname "test_log"
append log_fname "_$fname_time"
append log_fname "_$fname_date"
append log_fname ".dat"

puts $log_fname

Short form

set log_fname "life_test_log_[clock format [clock seconds] -format %H%M%S]_[clock format [clock seconds] -format %d%m%y].dat"

puts $log_fname

See https://www.tcl.tk/man/tcl8.4/TclCmd/clock.html

  • Related