I want to change clock from 12h to 24h mode via terminal

So I'm looking for the command to change from 12h to 24h. Is there even such a command? I mean I can do it with the GUI but I need to be able to do it via terminal so I can put it in a bash script.

Here I want instead of the 12h format a 24h format.


Solution 1:

This is stored in the plist located at ~/Library/Preferences/com.apple.menuextra.clock under the key DateFormat.

Here's a quick primer:

  • 12 Hour Mode with AM/PM:

    EEE MMM d h:mm:ss a

  • 12 Hour Mode without AM/PM:

    EEE MMM d h:mm:ss

  • 24 Hour Mode:

    EEE MMM d H:mm:ss

To apply any of these formatting strings, you can either use the defaults command at the Terminal, or if you're looking for a more programmable method, you can use the do shell script1 directive within Applescript.

To apply these via the Terminal, open it up and run the following command:

defaults write com.apple.menuextra.clock DateFormat -string 'EEE MMM d  H:mm:ss'

Substitute the string between the quotes with the version from above that you want.

For the changes to take immediate effect, kill -SIGHUP SystemUIServer will do the trick. Otherwise they will take effect the next time you log out and back in or restart.


1CyberCiti Primer on Shell Scripting via Applescript