Category filter

Execute custom scripts to set date and time for macOS devices

Changing the date or time on a Mac requires the user to customize the Date & Time settings from the System Preferences or System Settings. Enforcing a date and time is extremely critical in enterprise settings where accurate timestamps are necessary for logging and tracking. Setting the date and time on macOS devices deployed for work can be efficiently automated using scripts. As an administrator, if you want to force the date or time on Macs without involving repetitive user interaction, you can execute the following scripts using the
Execute Custom Script
action.

Scripting language – Bash

File extension – .sh

Disclaimer:

The sample scripts provided below are adapted from third-party open-source sites.

Set Date and Time

On macOS, the shell scripting language commonly used is Bash. Here’s an example of a Bash script:

Suppose to set the date and time of a device to 6 December 2023 2:30 pm, the command takes the following form:
date 120614302023.00

The basic script presented above uses the date command. The date command is followed by the value that specifies the date and time in the required format. The format used is MMDDhhmm[[CC]YY][.ss], where:

MM – the month of the year

DD – the date of the month

hh – the hour

mm – the minutes

CC – the first two digits of the year (century)

YY – the last two digits of the year

ss – the seconds

You may also use the following script if you want to pass the value for date and time as arguments. While executing the script, you may specify the date and time in the same format MMDDhhmm[[CC]YY][.ss] in the Arguments field.

Execute custom scripts to set date and time on macOS devices

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository