# 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](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) 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:

Sample Script to set date and time on Mac

\#!/bin/sh date 120614302023.00 

   1

2

3

  \#!/bin/sh 

date 120614302023.00 

   

 

  

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](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/12/Execute-script-to-set-date-and-time-on-a-macOS-device-from-command-line.png "Execute script to set date and time on a macOS device from command line ")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/12/Execute-script-to-set-date-and-time-on-a-macOS-device-from-command-line.png)

Sample Script to set date and time on Mac

\#!/bin/sh date $1 

   1

2

  \#!/bin/sh 

date $1 

   

 

  

You can verify the status of the script execution from the *Action History* tab of the device in the Hexnode UEM portal. Navigate to the **Manage** tab and click on the device. Along with the *Device Summary* sub-tab, there are other sub-tabs, including *Action History*. Go to *Action History* and verify the status of the given action.

 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.