Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

converting milliseconds into proper time format

VigneshMC
Mega Sage

Any idea on how to convert the millisecond value into hrs min and sec format using client script?

5 REPLIES 5

Osterber
Tera Contributor

In my scenario i had to get the lastLogon ticks from Active Directory and store it in the u_description field in sys_user and this is what i ended up with. this is intended for a transform map.

 

answer = (function transformEntry(source) {

/**
 * Script to convert the 'lastLogon' timestamp from Active Directory (stored in ticks) 
 * into a human-readable date format and update the 'description' field of a sys_user record.
 * 
 * If the 'description' field already contains the last logon information, the new timestamp 
 * will not be added again. If there is no existing last logon information, the script will 
 * append the new logon information to the 'description' field.
 * 
 * The script performs the following tasks:
 * 1. Retrieves the 'lastLogon' timestamp (in ticks) from Active Directory data.
 * 2. Converts the ticks into milliseconds (Unix timestamp).
 * 3. Creates a GlideDateTime object to format the timestamp.
 * 4. Checks if the 'description' field already contains the same logon information.
 * 5. If not, appends the formatted logon information to the 'description' field.
 * 
 * Assumptions:
 * - The 'lastLogon' value is stored in the 'u_lastlogon' field in the incoming data (e.g., from AD).
 * - The 'description' field exists on the target sys_user record.
 * 
 * Author: [Robert Österberg]
 * Date: [2025-01]
 */

	// Add your code here


    // Constants for ticks conversion
    var TICKS_IN_MILLISECOND = 10000;  // 1 tick = 100 nanoseconds = 0.0001 milliseconds
    var WINDOWS_EPOCH = 116444736000000000;  // Windows epoch (January 1, 1970)

    // Get the value of the lastLogon field from the imported Active Directory data
    var lastLogonTicks = source.u_lastlogon;  // Assuming 'u_lastlogon' is the field from AD
	//var lastLogonTicks = 133801265253673148;
    
	// Check if lastLogonTicks is undefined or null
    if (lastLogonTicks == undefined || lastLogonTicks == null) {
        gs.error('lastLogon field is empty or not present. Value: ' + lastLogonTicks);
        return;
    }

    // Log the raw ticks value for debugging purposes
    gs.info('Raw lastLogon (Ticks): ' + lastLogonTicks);

    // Convert the ticks to milliseconds (Windows FileTime to Unix epoch)
    var milliseconds = (lastLogonTicks - WINDOWS_EPOCH) / TICKS_IN_MILLISECOND;

    // Log the milliseconds value to check if the conversion is correct
    gs.info('Converted milliseconds: ' + milliseconds);

    // Check if the milliseconds value is valid
    if (isNaN(milliseconds) || milliseconds <= 0) {
        gs.error('Invalid lastLogon timestamp: ' + lastLogonTicks + ' or conversion failed.');
        return;
    }

    // Round milliseconds to ensure it's an integer (remove fractional part)
    milliseconds = Math.floor(milliseconds);  // Rounds down the value to the nearest integer

    // Log the rounded milliseconds value for confirmation
    gs.info('Rounded milliseconds value for GlideDateTime: ' + milliseconds);

    // Attempt to create a GlideDateTime object using the milliseconds value
    try {
        // Attempt using the milliseconds value in the Unix epoch format
        var glideDateTime = new GlideDateTime();
        glideDateTime.setValue(milliseconds);  // Setting value using Unix milliseconds format
        gs.info('GlideDateTime created successfully using milliseconds: ' + glideDateTime.getValue());
    } catch (e) {
        gs.error('Failed to create GlideDateTime using milliseconds: ' + e);
        return;
    }

    // Check if GlideDateTime is successfully created
    if (!glideDateTime) {
        gs.error('Failed to create a valid GlideDateTime object.');
        return;
    }

    // Log the GlideDateTime object value (this should be in UTC format)
    gs.info('GlideDateTime value: ' + glideDateTime.getValue());

    // Format the date as a human-readable string
    var formattedDate = glideDateTime.getDisplayValue();  // Adjust this for your desired format

    // Log the formatted date for debugging purposes
    gs.info('Formatted lastLogon date: ' + formattedDate);

	// Check if there's already a value in the description field
    var currentDescription = target.u_description;  // Get the current value of the description field
    var newLogonInfo = 'Last Logon: ' + formattedDate;

    // If there is already a value in the description field
    if (currentDescription) {
        // Compare the new logon info with the existing description to check if it's different
        if (currentDescription.indexOf(newLogonInfo) === -1) {
            // Append the new logon info if it's not already present
            target.u_description = currentDescription + '\n' + newLogonInfo;
            gs.info('Updated description: ' + target.description);  // Log the updated description
        } else {
            gs.info('No update needed. Last logon info already present.');
        }
    } else {
        // Set the new description if it's empty
        target.u_description = newLogonInfo;
        gs.info('Description set to: ' + target.u_description);  // Log the new description
    }

	return target.u_description; // return the value to be put into the target field

})(source);