Discovering the most excellent format_date Drupal 8 migrate process plugin

I often work on migrations that involved dates that need to be migrated into Drupal 8. While many times the dates are for entity created and updated dates, and therefore in Unix timestamp format, sometimes (when migrating events, for example), I'll need to migrate a date in some other format into Drupal's date field.

In the past, I've ended up writing a custom process plugin or callback to convert the date into the proper format for Drupal core's date field, but I recently discovered the format_date process plugin (I'm pretty sure I'm late to the party on this) and realized I was doing more work than I had to. 

The short version is this - the format_date plugin takes a date in any format (as long as it can be described using the standard PHP Date patterns) and converts it to the format Drupal requires. Oh, and it also has timezone support!

Here's an example of taking a datetime in the format of "11/04/18 08:30:00" (EST) and converting it to the format that Drupal's core date field requires, "2019-11-04T08:30:00" (UTC).

field_eventdate/value:
  plugin: format_date
  from_format: 'm/d/y H:i:s'
  to_format: 'Y-m-d\TH:i:s'
  from_timezone: 'America/New_York'
  to_timezone: 'UTC'

It's pretty simple! Less custom code usually means less opportunities for mistakes and less code to maintain in the future!

Add new comment

The content of this field is kept private and will not be shown publicly.