---
title: "string(for:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/datecomponentsformatter/string(for:)"
---

# string(for:)

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

## Declaration

```swift
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 doc://com.apple.foundation/documentation/Foundation/NSDateComponents object; if it is not, the method raises an exception. This parameter must not be nil.

## Return Value

Return Value A formatted string representing the specified date information.

## Discussion

Discussion This method has the same behavior as the string(from:) method.

## See Also

### Formatting Values

- [string(from:)](foundation/datecomponentsformatter/string(from:)-9exxn.md)
- [string(from:to:)](foundation/datecomponentsformatter/string(from:to:).md)
- [string(from:)](foundation/datecomponentsformatter/string(from:)-7sj4j.md)
- [localizedString(from:unitsStyle:)](foundation/datecomponentsformatter/localizedstring(from:unitsstyle:).md)
