# Timespan

**URL:** <https://forums.powershell.org/t/timespan/8264>\
**Category:** PowerShell Help\
**Created:** [March 21, 2017, 9:00am UTC](https://forums.powershell.org/t/timespan/8264 "2017-03-21T09:00:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![balajirk](https://avatars.discourse-cdn.com/v4/letter/b/e19b73/32.png) [@balajirk](https://forums.powershell.org/u/balajirk)\
**Post date:** [March 21, 2017, 9:00am UTC](https://forums.powershell.org/t/timespan/8264/1 "2017-03-21T09:00:11Z")

</div>

could anyone clarify me on this question.

> Are there any difference between syntax **[Timespan]$var = new-timespan & $var = new-timespan?**

if yes what is the difference.

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [March 21, 2017, 9:30am UTC](https://forums.powershell.org/t/timespan/8264/2 "2017-03-21T09:30:37Z")

</div>

When using:

```
[Timespan]$var = new-timespan
```

You’re specifically spelling out (casting) the $var variable type, as System.Timespan data type. Whereas,

```
$var = new-timespan
```

You’re letting PowerShell determine what data type to assign to $var.

PowerShell usually does a good job at assigning the right data type, but in some cases you may want to specify exactly the data type to use.

For example:

```
[int]1 + [int]1
```

returns 1, while

```
[string]1 + [string]1
```

returns 11 🙂

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [March 21, 2017, 12:09pm UTC](https://forums.powershell.org/t/timespan/8264/3 "2017-03-21T12:09:37Z")

</div>

To answer your question directly - NO

```
PS> [timespan]$var = New-TimeSpan
PS> $var.GetType().Fullname
System.TimeSpan
PS>
PS> $var1 = New-TimeSpan
PS> $var1.GetType().Fullname
System.TimeSpan
```

As the code shows in both cases you have a Timespan object. PowerShell is very good at managing the type of the objects it deals with. In your first case the cast to TimeSpan you do is actually redundant because New-Timespan creates a timespan object. The cast is therefore ignored.

As a generalisation when you create an object from a cmdlet you don’t need to cast the variable to that type.

You can test the type of any object

```
PS> $var -is [timespan]
True
PS> $var1 -is [timespan]
True
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/timespan/8264/4 "2024-05-16T20:39:03Z")

</div>

Closed on May 16, 2024, 8:39pm UTC


