Parsing the Date and Time Sections from a Date/Time field

Here are some examples of how you could use a simple Javascript ‘split’ method to parse out the date and/or time value from a date/time field.  Both examples split out the date and time, populate them into variables, and then populate the date section into a regular date field on the same record.

Business rule example:

This could also be used in a graphical workflow ‘Run script’ activity but would need to include ‘current.update()’ at the end to call the update to the record.

Name: Populate Date from Date/Time
When: Before
Update: True
Condition: current.u_start_date_time.changes()

var dateSection = current.u_start_date_time.split(' ')[0]; //Gets the Date
var timeSection = current.u_start_date_time.split(' ')[1]; //Gets the Time
//Set the value of the Date field with the date from the Date/Time field
current.u_start_date = dateSection;

Client script example:

As is the case with all client scripting in Service-now, all fields referenced must actually be part of the rendered form (though they may be invisible) to be referenced by the client-side script.

Name: Populate Date from Date/Time
Type: OnChange
Field name: Start date/time (or whatever your date/time field is named)

function onChange(control, oldValue, newValue, isLoading) {
  //If the page isn't loading
  if (!isLoading) {
    //If the new value isn't blank
    if(newValue != '') {
      var dateSection = g_form.getValue('u_start_date_time').split(' ')[0]; //Gets the Date
      var timeSection = g_form.getValue('u_start_date_time').split(' ')[1]; //Gets the Time
      //Set the value of the Date field with the date from the Date/Time field
      g_form.setValue('u_start_date', dateSection);
    }
  }
}

Date Posted:

January 11, 2010

Share This:

3 Comments

  1. Anonymous January 16, 2010 at 2:10 am

    In my environment while working with dates I prefer to use date/time format stored in property. Below script is used in one of my script includes which converts expected start date into date format required by the underlying system.

     
    // This Function takes the expected start date value from change_task and convert it as per bladelogic format 
    formatBLDeployDate: function(deployDate){ 
    // Getting date and time format as per system properties 
    var inputformat = gs.getProperty('glide.sys.date_format') + ' ' + gs.getProperty('glide.sys.time_format'); 
    var lv_parser = new java.text.SimpleDateFormat(inputformat); 
    // System stores date in UTC format. 
    lv_parser.setTimeZone(java.util.TimeZone.getTimeZone('UTC')); 
    // Date format for Bladelogic this date is split latter for date and time 
    var lv_formatter = new java.text.SimpleDateFormat('MM/dd/yyyy HH:mm:ss'); 
    // Setting system time zone which is US/Eastern 
    lv_formatter.setTimeZone(java.util.TimeZone.getTimeZone(gs.getProperty('glide.sys.default.tz'))); 
    // Parse the date and convert it into Date object 
    var lv_localDate = lv_parser.parse(deployDate); 
    // Convert date in local time zone format 
    lv_dateFormateInLocalTimeZone = lv_formatter.format(lv_localDate); 
    gs.log('Formated Date for Bl --&> '+ lv_dateFormateInLocalTimeZone); 
    return lv_dateFormateInLocalTimeZone; 
     
    }, 
    
    • valor September 10, 2012 at 7:36 pm

      Thanks, Amit!
      I made it a bit more reusable for people:
      [code]
      formatDate = function (dateDisplay, desiredFormat){
      if (!desiredFormat){
      desiredFormat = “dd-MMM-yyyy”;
      }

      // build Java date objects
      var inputformat = gs.getProperty(‘glide.sys.date_format’) + ”;
      var jDateFormat = new Packages.java.text.SimpleDateFormat(inputformat);
      var jDesiredDateFormat = new Packages.java.text.SimpleDateFormat(desiredFormat);

      // assume “dateDisplay” is in current user’s TZ
      jDateFormat.setTimeZone(Packages.java.util.TimeZone.getTimeZone(gs.getUser().getTZ()));
      // assume output will be in system TZ, regardless of user
      jDesiredDateFormat.setTimeZone(Packages.java.util.TimeZone.getTimeZone(gs.getProperty(‘glide.sys.default.tz’)));

      // Create Java date object based off of input
      var jInDate = jDateFormat.parse(dateDisplay);
      // Create converted Java date obj
      var jOutDate = jDesiredDateFormat.format(jInDate);

      // return a formatted String-primitive
      return jOutDate + ”;
      }[/code]

  2. ND December 30, 2013 at 2:44 pm

    Hi.

    Thanks for the useful code, I checked and it’s working fine in Business rule.

    But when I try to implement same in Script include, it’s not working. I want to create a script include function and call it in client script.

    regards,
    ND

Comments are closed.

Categories

Tags

Loading

Fresh Content
Direct to Your Inbox

Just add your email and hit subscribe to stay informed.