toInstantOrNull

fun String.toInstantOrNull(pattern: String = "EEE MMM dd HH:mm:ss zzz yyyy"): Instant?

Produce an Instant from the given strings value and pattern if possible.

Example:

"8/7/2022".toInstantOrNull("MM/dd/yyyy") // 2022-08-06T19:30:00Z
"Sun Aug 07 16:37:42 IRDT 2022".toInstantOrNull() // 2022-08-07T12:07:42Z
"7/2022".toInstantOrNull("MM/dd/yyyy") // null

Since

1.1.0

Return

A nullable Instant parsed from the string.

See also