0

I am trying to get the current date in CST format in linux.

todaysDate=$(date +%Y-%m-%d-%H:%M:%S);
echo 'todaysDate: '${todaysDate}; 

Result: todaysDate: 2022-06-03-09:15:42

is giving me time in UTC. How can I get the time in CST instead, without changing the system time settings?

Thanks in advance

Jenz
  • 41
  • 5

2 Answers2

2

See this answer on the Unix stack exchange. You basically prepend an environment variable that is only valid for the very command you're trying to issue:

$(TZ=<Your Timezone> date +%Y-%m-%d-%H:%M:%S)

All available timezones are listed in /usr/share/zoneinfo.

YoshiMbele
  • 767
  • 1
  • 12
0

Set the environment variable TZ to CST6CDT.

todaysDate=$(TZ=CST6CDT date +%Y-%m-%d-%H:%M:%S);
Mime
  • 1,142
  • 1
  • 9
  • 20