# Class TimeExtensions (3.27.1)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [3.27.1 (latest)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.TimeExtensions)
- [3.23.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.23.0/Google.Protobuf.WellKnownTypes.TimeExtensions)
- [3.15.8](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.15.8/Google.Protobuf.WellKnownTypes.TimeExtensions)

    public static class TimeExtensions

Extension methods on BCL time-related types, converting to protobuf types.

## Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) \> TimeExtensions

## Inherited Members

[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)) [object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)) [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) [object.MemberwiseClone()](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone) [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals) [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Namespace

[Google.Protobuf.WellKnownTypes](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes)

## Assembly

Google.Protobuf.dll

## Methods

### ToDuration(TimeSpan)

    public static Duration ToDuration(this TimeSpan timeSpan)

Converts the given [TimeSpan](https://learn.microsoft.com/dotnet/api/system.timespan) to a [Duration](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.Duration).

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `timeSpan` | `https://learn.microsoft.com/dotnet/api/system.timespan` The time span to convert. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.Duration` | The converted duration. |

### ToTimestamp(DateTime)

    public static Timestamp ToTimestamp(this DateTime dateTime)

Converts the given [DateTime](https://learn.microsoft.com/dotnet/api/system.datetime) to a [Timestamp](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.Timestamp).

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `dateTime` | `https://learn.microsoft.com/dotnet/api/system.datetime` The date and time to convert to a timestamp. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.Timestamp` | The converted timestamp. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.argumentexception` | The `dateTime` value has a [Kind](https://learn.microsoft.com/dotnet/api/system.datetime.kind)other than `Utc`. |

### ToTimestamp(DateTimeOffset)

    public static Timestamp ToTimestamp(this DateTimeOffset dateTimeOffset)

Converts the given [DateTimeOffset](https://learn.microsoft.com/dotnet/api/system.datetimeoffset) to a [Timestamp](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.Timestamp)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `dateTimeOffset` | `https://learn.microsoft.com/dotnet/api/system.datetimeoffset` The date and time (with UTC offset) to convert to a timestamp. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.Timestamp` | The converted timestamp. |

**Remarks** The offset is taken into consideration when converting the value (so the same instant in time
is represented) but is not a separate part of the resulting value. In other words, there is no
roundtrip operation to retrieve the original `DateTimeOffset`.