SharePoint Designer 2010 workflow to set time portion of a date/time

Learn how you can concatenate a date and a time, and use date/time conversions to and from UTC to be able to set the time portion of a date/time field in a SharePoint list column through a SharePoint Designer 2010 workflow.

ADVERTISEMENTS

In Construct a date from strings in a SharePoint Designer 2010 workflow you saw how to create a date by concatenating two strings in a SharePoint Designer 2010 workflow.

In the SharePoint Designer 2010 workflow in this article, you will set the time portion of a date and time field in a SharePoint list by concatenating a date and a time taken from two fields in the SharePoint list.

For this solution, you must create a SharePoint list that has 3 columns:

  1. A column with the name myDate, type Date and Time, and which has its settings to only display the date, so that no time can be entered.
  2. A column with the name myTime and type Single line of text. In this field you will enter a time that has the format hh:mm.
  3. A column with the name myDateAndTime, type Date and Time, and which has its settings to display both the date and time. In this field you will store the result of the workflow.

To create a SharePoint Designer 2010 workflow that sets the time portion of a date/time field in a SharePoint list:

  1. In SharePoint Designer 2010, create a new List Workflow.
  2. Add the following Local Variables to the SharePoint Designer 2010 workflow:
    • myDateAndTime (Date/Time)
    • dateString (String)
    • timeString (String)
    • isoDateTimeString (String)
  3. Add a Set Workflow Variable action to the workflow to set the myDateAndTime workflow variable to the myDate field of the Current Item in the SharePoint list.
  4. Add a second Set Workflow Variable action to the workflow to set the dateString workflow variable to the myDateAndTime workflow variable and return the field as ISO Formatted. Here you are converting the date and time variable to an ISO date and time string.
  5. Add an Extract Substring from Start of String action to the workflow that copies 10 characters from the dateString workflow variable, and outputs it to the same dateString workflow variable.
  6. Add a third Set Workflow Variable action to the workflow to set the timeString workflow variable to the myTime field of the Current Item in the SharePoint list.
  7. Add a fourth Set Workflow Variable action to the workflow to set the isoDateTimeString workflow variable to the following string: [%Variable: dateString%]T[%Variable: timeString%]:00Z using the String Builder dialog box.
  8. Add a fifth Set Workflow Variable action to the workflow to set the myDateAndTime workflow variable to the isoDateTimeString workflow variable and return the field as Date/Time. Here you are converting the ISO date and time string back to a date and time variable.
  9. Add an Add Time to Date action to the workflow to add an appropriate amount of hours for the time zone you live in to the myDateAndTime workflow variable, and output it to the same myDateAndTime workflow variable. This time zone correction is necessary, because when you retrieved the date from the SharePoint list column you converted it to an ISO date and time string, which represents dates and times in UTC. So before you write the date and time back to the SharePoint list, you must convert the date and time from UTC back to your own time zone. You must also subtract an hour for daylight savings time if daylight savings is currently in effect for your time zone.
  10. Add a Set Field in Current Item action to the workflow that sets the myDateAndTime column of the SharePoint list to the value of the myDateAndTime workflow variable.

    The SharePoint Designer workflow should now resemble the following figure:

    SharePoint Designer 2010 workflow to set the time portion of a date/time column in a SharePoint list
    Figure 1. SharePoint Designer 2010 workflow to set the time portion of a date/time column in a SharePoint list.

  11. Publish the workflow to SharePoint.

If you go to the SharePoint list to which you associated the workflow, add an item to the SharePoint list, enter a date in the myDate column, enter a time in the myTime column, save the SharePoint list item, and then start the workflow on the item, the myDateAndTime column of the SharePoint list should be updated and display the correct date and time, which is a concatenation of the date and time you entered in the two other columns.

Related Articles

ADVERTISEMENTS