diff --git a/core/lib/Drupal/Component/Datetime/DateTimePlus.php b/core/lib/Drupal/Component/Datetime/DateTimePlus.php
index 44c0e65..114d68c 100644
--- a/core/lib/Drupal/Component/Datetime/DateTimePlus.php
+++ b/core/lib/Drupal/Component/Datetime/DateTimePlus.php
@@ -278,7 +278,7 @@ public function __construct($time = 'now', $timezone = NULL, $settings = array()
    *
    * @return string
    */
-  protected function render() {
+  public function render() {
     return $this->format(static::FORMAT) . ' ' . $this->getTimeZone()->getName();
   }
 
diff --git a/core/lib/Drupal/Core/Datetime/DateFormatter.php b/core/lib/Drupal/Core/Datetime/DateFormatter.php
index fd445e3..0262a34 100644
--- a/core/lib/Drupal/Core/Datetime/DateFormatter.php
+++ b/core/lib/Drupal/Core/Datetime/DateFormatter.php
@@ -7,7 +7,6 @@
 
 namespace Drupal\Core\Datetime;
 
-use Drupal\Component\Utility\Xss;
 use Drupal\Core\Config\ConfigFactoryInterface;
 use Drupal\Core\Datetime\DrupalDateTime;
 use Drupal\Core\Entity\EntityManagerInterface;
@@ -130,8 +129,9 @@ public function __construct(EntityManagerInterface $entity_manager, LanguageMana
    *   (optional) Language code to translate to. NULL (default) means to use
    *   the user interface language for the page.
    *
-   * @return string
-   *   A translated date string in the requested format.
+   * @return string|\Drupal\Component\Utility\SafeStringInterface
+   *   A translated date string in the requested format. Since the format may
+   *   contain user input, this value should be escaped when output.
    */
   public function format($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
     if (!isset($timezone)) {
@@ -168,7 +168,7 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N
     $settings = array(
       'langcode' => $langcode,
     );
-    return Xss::filter($date->format($format, $settings));
+    return $date->format($format, $settings);
   }
 
   /**
diff --git a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
index e7c71f1..a45d1fa 100644
--- a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
+++ b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
@@ -89,8 +89,9 @@ protected function prepareTimezone($timezone) {
    *   - langcode: (optional) String two letter language code used to control
    *     the result of the format() method. Defaults to NULL.
    *
-   * @return string
-   *   The formatted value of the date.
+   * @return string|\Drupal\Component\Utility\SafeStringInterface
+   *   The formatted value of the date. Since the format may contain user input,
+   *   this value should be escaped when output.
    */
   public function format($format, $settings = array()) {
     $langcode = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode;
diff --git a/core/lib/Drupal/Core/Datetime/Element/Datetime.php b/core/lib/Drupal/Core/Datetime/Element/Datetime.php
index f9ccb45..3eb7c84 100644
--- a/core/lib/Drupal/Core/Datetime/Element/Datetime.php
+++ b/core/lib/Drupal/Core/Datetime/Element/Datetime.php
@@ -241,7 +241,7 @@ public static function processDatetime(&$element, FormStateInterface $form_state
       // placeholders are invalid for HTML5 date and datetime, so an example
       // format is appended to the title to appear in tooltips.
       $extra_attributes = array(
-        'title' => t('Date (e.g. !format)', array('!format' => static::formatExample($date_format))),
+        'title' => t('Date (e.g. @format)', array('@format' => static::formatExample($date_format))),
         'type' => $element['#date_date_element'],
       );
 
@@ -288,7 +288,7 @@ public static function processDatetime(&$element, FormStateInterface $form_state
 
       // Adds the HTML5 attributes.
       $extra_attributes = array(
-        'title' => t('Time (e.g. !format)', array('!format' => static::formatExample($time_format))),
+        'title' => t('Time (e.g. @format)', array('@format' => static::formatExample($time_format))),
         'type' => $element['#date_time_element'],
         'step' => $element['#date_increment'],
       );
diff --git a/core/lib/Drupal/Core/Extension/module.api.php b/core/lib/Drupal/Core/Extension/module.api.php
index fef4c32..45ecf53 100644
--- a/core/lib/Drupal/Core/Extension/module.api.php
+++ b/core/lib/Drupal/Core/Extension/module.api.php
@@ -946,7 +946,7 @@ function hook_requirements($phase) {
     $cron_last = \Drupal::state()->get('system.cron_last');
 
     if (is_numeric($cron_last)) {
-      $requirements['cron']['value'] = t('Last run !time ago', array('!time' => \Drupal::service('date.formatter')->formatTimeDiffSince($cron_last)));
+      $requirements['cron']['value'] = t('Last run @time ago', array('@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($cron_last)));
     }
     else {
       $requirements['cron'] = array(
diff --git a/core/modules/system/src/Tests/Common/FormatDateTest.php b/core/modules/system/src/Tests/Common/FormatDateTest.php
index 0ece458..6455018 100644
--- a/core/modules/system/src/Tests/Common/FormatDateTest.php
+++ b/core/modules/system/src/Tests/Common/FormatDateTest.php
@@ -117,5 +117,10 @@ function testFormatDate() {
     $this->assertIdentical(format_date($timestamp, 'html_week'), '2007-W12', 'Test html_week date format.');
     $this->assertIdentical(format_date($timestamp, 'html_month'), '2007-03', 'Test html_month date format.');
     $this->assertIdentical(format_date($timestamp, 'html_year'), '2007', 'Test html_year date format.');
+
+    // HTML is not escaped by the date formatter, it must be escaped later.
+    $formatter = \Drupal::service('date.formatter');
+    $this->assertIdentical($formatter->format($timestamp, 'custom', '\<\s\c\r\i\p\t\>\a\l\e\r\t\(\'Y\'\)\;\<\/\s\c\r\i\p\t\>'), "<script>alert('2007');</script>", 'Script tags not removed from dates.');
+    $this->assertIdentical($formatter->format($timestamp, 'custom', '\<\e\m\>Y\<\/\e\m\>'), '<em>2007</em>', 'Em tags are not removed from dates.');
   }
 }
diff --git a/core/modules/system/src/Tests/System/DateTimeTest.php b/core/modules/system/src/Tests/System/DateTimeTest.php
index 39dac52..b2a5d2f 100644
--- a/core/modules/system/src/Tests/System/DateTimeTest.php
+++ b/core/modules/system/src/Tests/System/DateTimeTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\system\Tests\System;
 
+use Drupal\Core\Url;
 use Drupal\simpletest\WebTestBase;
 
 /**
@@ -133,20 +134,31 @@ function testDateFormatConfiguration() {
     $this->assertText(t('Custom date format added.'), 'Date format added confirmation message appears.');
     $this->assertText($name, 'Custom date format appears in the date format list.');
     $this->assertText(t('Delete'), 'Delete link for custom date format appears.');
-  }
 
-  /**
-   * Test that date formats are sanitized.
-   */
-  function testDateFormatXSS() {
     $date_format = entity_create('date_format', array(
       'id' => 'xss_short',
       'label' => 'XSS format',
       'pattern' => '\<\s\c\r\i\p\t\>\a\l\e\r\t\(\'\X\S\S\'\)\;\<\/\s\c\r\i\p\t\>',
-    ));
+      ));
     $date_format->save();
 
-    $this->drupalGet('admin/config/regional/date-time');
-    $this->assertNoRaw("<script>alert('XSS');</script>", 'The date format was properly sanitized');
+    $this->drupalGet(Url::fromRoute('entity.date_format.collection'));
+    $this->assertEscaped("<script>alert('XSS');</script>", 'The date format was properly escaped');
+
+    // Add a new date format with HTML in it.
+    $date_format_id = strtolower($this->randomMachineName(8));
+    $name = ucwords($date_format_id);
+    $date_format = '& \<\e\m\>Y\<\/\e\m\>';
+    $edit = array(
+      'id' => $date_format_id,
+      'label' => $name,
+      'date_format_pattern' => $date_format,
+    );
+    $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
+    $this->assertUrl(\Drupal::url('entity.date_format.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
+    $this->assertText(t('Custom date format added.'), 'Date format added confirmation message appears.');
+    $this->assertText($name, 'Custom date format appears in the date format list.');
+    $this->assertEscaped('<em>' . date("Y") . '</em>');
   }
+
 }
diff --git a/core/modules/system/system.install b/core/modules/system/system.install
index c168b24..563a119 100644
--- a/core/modules/system/system.install
+++ b/core/modules/system/system.install
@@ -464,7 +464,7 @@ function system_requirements($phase) {
     }
 
     // Set summary and description based on values determined above.
-    $summary = t('Last run !time ago', array('!time' => \Drupal::service('date.formatter')->formatTimeDiffSince($cron_last)));
+    $summary = t('Last run @time ago', array('@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($cron_last)));
 
     $requirements['cron'] = array(
       'title' => t('Cron maintenance tasks'),
diff --git a/core/modules/tracker/tracker.pages.inc b/core/modules/tracker/tracker.pages.inc
index 47bd3ef..68c0667 100644
--- a/core/modules/tracker/tracker.pages.inc
+++ b/core/modules/tracker/tracker.pages.inc
@@ -106,8 +106,8 @@ function tracker_page($account = NULL) {
           'data-history-node-last-comment-timestamp' => $node->last_comment_timestamp,
         ),
         'last updated' => array(
-          'data' => t('!time ago', array(
-            '!time' => \Drupal::service('date.formatter')->formatTimeDiffSince($node->last_activity),
+          'data' => t('@time ago', array(
+            '@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($node->last_activity),
           )),
         ),
       );
diff --git a/core/modules/views_ui/src/ViewEditForm.php b/core/modules/views_ui/src/ViewEditForm.php
index 1b335de..cad9be5 100644
--- a/core/modules/views_ui/src/ViewEditForm.php
+++ b/core/modules/views_ui/src/ViewEditForm.php
@@ -140,13 +140,13 @@ public function form(array $form, FormStateInterface $form_state) {
       );
       $lock_message_substitutions = array(
         '!user' => drupal_render($username),
-        '!age' => $this->dateFormatter->formatTimeDiffSince($view->lock->updated),
+        '@age' => $this->dateFormatter->formatTimeDiffSince($view->lock->updated),
         '@url' => $view->url('break-lock-form'),
       );
       $form['locked'] = array(
         '#type' => 'container',
         '#attributes' => array('class' => array('view-locked', 'messages', 'messages--warning')),
-        '#children' => $this->t('This view is being edited by user !user, and is therefore locked from editing by others. This lock is !age old. Click here to <a href="@url">break this lock</a>.', $lock_message_substitutions),
+        '#children' => $this->t('This view is being edited by user !user, and is therefore locked from editing by others. This lock is @age old. Click here to <a href="@url">break this lock</a>.', $lock_message_substitutions),
         '#weight' => -10,
       );
     }
