Programmatically Set the System Time in LabVIEW

Updated Sep 20, 2023

Environment

Software

  • LabVIEW Base
  • LabVIEW Full

Operating System

  • Windows

I am reading GPS time from an instrument and would like to set the Windows system time at a high resolution from LabVIEW. I've noticed the Get Time/Date VIs on the time palette in LabVIEW, but I cannot find any "set time" VIs.

There are no built in LabVIEW VIs to set the system time. However, this can be accomplished using the Windows SDK. Here are the instructions to use a Call Library Function Node to call the SetSystemTime function from the kernel32.dll:
  1. Place a Call Library Function Node on the block diagram. Double-click the node to open the Call Library Function dialog box.
  2. Click the Browse button and select the kernel32.dll (usually found at C:\WINDOWS\system32\kernel32.dll").
  3. Select SetSystemTime from the Function Name drop-down list.
  4. Select stdcall (WINAPI) from the Calling Conventions drop-down list.
  5. For Return Type select "Numeric" and select "Signed 8-bit Integer".
  6. Click Add a Parameter After and for Type select "Adapt to Type" and for Data Format select "Pointers to Handles".
  7. Click the OK button to configure the Call Library Function Node.
  8. This function takes a structure of eight WORDS (16-bit integers) as a parameter. To replicate this in LabVIEW, create a cluster of eight WORDS. The eight WORDS represent the Year, Month, Day of Week, Day of Month, Hour, Minute, Second, and Millisecond. Wire the cluster to the param 1 input of the Call Library Function Node.
  9. Enter the desired values and run the program to set the system time.
Note: If you are calling the SetSystemTime from LabVIEW in Windows Vista or later, you must run LabVIEW as an administrator. To do this, right-click the LabVIEW icon and select Run as administrator.

Additional Information

Refer to the SetSystemTime documentation on msdn.com for more information on this function. Note that a GetSystemTime function also exists in kernel32.dll.

For more information about configuring the Call Library Function Node in LabVIEW 7.1 or earlier, refer to the Using External Code in LabVIEW manual. In LabVIEW 8.0 or later, refer to the Configuring the Call Library Function Node topic in the LabVIEW Help for more information.

Next Steps

You can circumvent the need for running the LabVIEW environment or your built application as an administrator by modifying the Group Policies on your computer. To do this follow these steps:
  1. Launch Edit group policy application on Windows by typing GPO in the Windows Start menu search box
  2. Navigate to Computer Configuration -> Windows Settings -> Security Settings -> Local Policies -> User Rights Assignment 
  3. Double click on the Change the System Time entry
  4. On the Local Security Setting tab click Add User or Group
  5. Type in the name of the user then hit Check Names
  6. Click Apply or OK as necessary
  7. Log out and then Log in and you should now be able to programmatically change the PC time