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!

Comments

Hey Mike, I'm working on a D7 --> D8 migration and have a content type called "event," which has a date field. After the migration, the date is off by one day (date imports 1 day earlier than the D7 date). I'm wondering if it's a time zone issue, but I'm not quite sure how to configure the migration to fix the issue.

Submitted by Chris (not verified) on Tue, 12/17/2019 - 13:43

Author comment

Anything date-related is tricky to debug - especially when timezones and/or daylight savings time gets in the mix.

I'd use xdebug and set a breakpoint in the format_date plugin...