<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/DateComponentsFormatter/string(for:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDateComponentsFormatter(im)stringForObjectValue:"
  },
  "title" : "string(for:)"
}
-->

# string(for:)

Returns a formatted string based on the date information in the specified object.

```
func string(for obj: Any?) -> String?
```

## Parameters

`obj`

An object containing the date and time information to format. The object in this parameter must be a [`NSDateComponents`](/documentation/Foundation/NSDateComponents) object; if it is not, the method raises an exception. This parameter must not be `nil`.

## Return Value

A formatted string representing the specified date information.

## Discussion

This method has the same behavior as the [`string(from:)`](/documentation/Foundation/DateComponentsFormatter/string(from:)-9exxn) method.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)