# Problem with get-date

**URL:** <https://forums.powershell.org/t/problem-with-get-date/575>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/problem-with-get-date/575 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/problem-with-get-date/575/1 "2011-12-31T18:30:00Z")

</div>

by gbritton at 2013-02-06 09:37:14

> Hi – I’m having trouble grasping how to use get-date in a function. e.g.  
>   
> `function d($f, $t) { $f;$t get-date $f get-date $t }`  
> But when I call the function like this:  
>   
> ` $a=get-date ‘1 jan 2013’ $b=get-date ‘31 jan 2013’ d($a,$b)`  
>   
> I get these messages:  
>   
> [list]Tuesday, January 01, 2013 12:00:00 AM  
> Thursday, January 31, 2013 12:00:00 AM  
> Get-Date : Cannot convert ‘System.Object’ to the type ‘System.DateTime’ requi  
> red by parameter ‘Date’. Specified method is not supported.  
> At line:4 char:10  
> + get-date \<\<\<\< $f  
> + CategoryInfo : InvalidArgument: (🙂 [Get-Date], ParameterBindin   
> gException  
> + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Comma   
> nds.GetDateCommand  
>    
> Get-Date : Cannot bind parameter ‘Date’ to the target. Exception setting "Date"  
> : "Object reference not set to an instance of an object."  
> At line:5 char:10  
> + get-date \<\<\<\< $t  
> + CategoryInfo : WriteError: (🙂 [Get-Date], ParameterBindingExce   
> ption  
> + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Comm   
> ands.GetDateCommand[/list]  
>   
> How can I use get-date in my toy function without generating errors?  
>   
> Note that, if I just issue get-date interactively, it works just fine:  
>   
> [list]  
> PS Microsoft.PowerShell.Core\FileSystem::\mcmsd-icms01\BillOps Data\Billing Operations\Tollse\> $a=get-date ‘1 jan 2013’  
> $b=get-date ‘31 jan 2013’  
>   
>   
> **PS Microsoft.PowerShell.Core\FileSystem::\mcmsd-icms01\BillOps Data\Billing Operations\Tollse\> get-date $a**  
>   
> **Tuesday, January 01, 2013 12:00:00 AM**  
>   
>   
>   
>   
> PS Microsoft.PowerShell.Core\FileSystem::\mcmsd-icms01\BillOps Data\Billing Operations\Tollse\> get-date $b  
>   
> Thursday, January 31, 2013 12:00:00 AM  
> [/list]

by ArtB0514 at 2013-02-06 10:17:21

> The problem is in how you are calling the function d. It is expecting 2 parameters, but by combining the input objects in parens, you are feeding it only one. And that one (going to the $f variable) is not in any format that Get-Date knows how to process, hence the first error message. The second error message comes from the $t variable being empty.  
>   
> Try it this way:  
>   
> `function d($f, $t) { $f;$t get-date $f get-date $t } $a=get-date '1 jan 2013' $b=get-date '31 jan 2013' d $a $b`

by ArtB0514 at 2013-02-06 10:30:11

> You can get lots of information from Get-Help About\_Functions (and the SEE ALSO topics at the bottom). Actually, use the -Online switch so you can get it all in a web browser and more easily navigate through it all.  
> `Get-Help About_Functions -Online`

by gbritton at 2013-02-06 10:43:03

> Aargh! You’re right! I’m still getting used to PowerShell’s unique syntax for function calling. I guess it’s too late to change that? FWIW many (most?) languages have some symmetry between a function’s signature and calling syntax. No wonder I’m confused…

by ArtB0514 at 2013-02-06 11:38:04

> The form you used was from PowerShell 1 and is no longer considered good practice. Look up help for About\_AdvancedFunctions. Here’s a simplified way to define a function using the more modern syntax that should help:  
>   
> `function d { param( [datetime]$f, [datetime]$t ) $f $t get-date $f get-date $t}`  
>   
> This way you call the function using either the BOA-Constructor method (d $a $b), or the parameter specific method (d -f $a -t $b)

by gbritton at 2013-02-06 12:15:54

> Thanks!

---

<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:58pm UTC](https://forums.powershell.org/t/problem-with-get-date/575/2 "2024-05-16T20:58:27Z")

</div>

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


