# Convert French date to US

**URL:** https://forums.powershell.org/t/convert-french-date-to-us/11758
**Category:** PowerShell Help
**Created:** [December 6, 2018, 7:39am UTC](https://forums.powershell.org/t/convert-french-date-to-us/11758 "2018-12-06T07:39:03Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![eliassal](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@eliassal](https://forums.powershell.org/u/eliassal)
#### Post date: [December 6, 2018, 7:39am UTC](https://forums.powershell.org/t/convert-french-date-to-us/11758/1 "2018-12-06T07:39:03Z")

</div>

Hi, I am trying to construct a string with file properties to insert in a table in SQL as follows

> select File\_name,Fully\_Qualified\_FileName,attributes, CreationTime, LastAccessTime, LastWriteTime,@{Label="Length";Expression={$\_.Length / 1MB -as [int] }}| ForEach-Object { $SQL = $sqlstatement -f $\_.FullName, $\_.name,$\_.attributes, $\_.CreationTime, $\_.LastAccessTime, $\_.LastWriteTime,$\_.Length Invoke-sqlcmd -Query $SQL -ServerInstance sql2k12 -database BaselineDB

As my files have all dates in French format and my sql server is US version, some records are not getting entered because it is is in the format dd/mm/yyyy. I created a function to convert French date to US date as follows

> function unction changeDataFormat { param( [Parameter(Position=0,mandatory=$true)] [datetime[]] $Dateorig ) $ukString = $Dateorig
> 
> $ukCulture = [Globalization.CultureInfo]‘fr-FR’  
> $usCulture = [Globalization.CultureInfo]‘en-US’
> 
> $datetime = [datetime]::Parse($ukString, $ukCulture)
> 
> $usString = $datetime.ToString(‘d’, $usCulture)  
> Write-Host $usString  
> return $usString  
> }

  
but when I try to use it I get a red swiggly line under the function indicating “Unexpected token **changeDataFormat** in expression or statement”

> select File\_name,Fully\_Qualified\_FileName,attributes, CreationTime, LastAccessTime, LastWriteTime,@{Label="Length";Expression={$\_.Length / 1MB -as [int] }}| ForEach-Object { $SQL = $sqlstatement -f $\_.FullName, $\_.name,$\_.attributes, **changeDataFormat** ($\_.CreationTime), $\_.LastAccessTime, $\_.LastWriteTime,$\_.Length Write-Host $SQL #Invoke-sqlcmd -Query $SQL -ServerInstance sql2k12 -database BaselineDB

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [December 6, 2018, 7:46am UTC](https://forums.powershell.org/t/convert-french-date-to-us/11758/2 "2018-12-06T07:46:05Z")

</div>

Please format your code as code using the “pre” tags, thanks.  
I think you should wrap your function call in parenthesis with a dollar sign before like this “**$(changeDataFormat $\_.CreationTime)**”

---

<div class="post-metadata">

### Author: ![eliassal](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@eliassal](https://forums.powershell.org/u/eliassal)
#### Post date: [December 6, 2018, 8:21am UTC](https://forums.powershell.org/t/convert-french-date-to-us/11758/3 "2018-12-06T08:21:07Z")

</div>

So many thanks, yes it is working like a charm. I will make sure to use Pre next time

---

<div class="post-metadata">

### Author: ![eliassal](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@eliassal](https://forums.powershell.org/u/eliassal)
#### Post date: [December 6, 2018, 8:41am UTC](https://forums.powershell.org/t/convert-french-date-to-us/11758/4 "2018-12-06T08:41:40Z")

</div>

One issue appeared, Creation date retrieved is in the following format and tried when converting to keep it Date and time

13/02/2017 10:04:07

I tried several Format Specifier on the line to convert but noting is getting through, I replaced the d with 'g", G, u…no one is going through

$usString = $datetime.ToString(‘d’, $usCulture)

Thanks

&nbsp;

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [December 6, 2018, 9:02am UTC](https://forums.powershell.org/t/convert-french-date-to-us/11758/5 "2018-12-06T09:02:41Z")

</div>

In your change function you specify to output your datetime as string. Why don’t you simpply return a datetime object. That would be more flexible and as I think more professional.

---

<div class="post-metadata">

### Author: ![eliassal](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@eliassal](https://forums.powershell.org/u/eliassal)
#### Post date: [December 6, 2018, 10:48am UTC](https://forums.powershell.org/t/convert-french-date-to-us/11758/6 "2018-12-06T10:48:29Z")

</div>

I did , added datetime type in the beginning

[datetime] $usString = $datetime.ToString(‘u’, $usCulture)

and now it is doing the conversion but with 1 hour in advance for the US time

13/02/2017 **10** :04:07  
02/13/2017 **11** :04:07

13/11/2016 **14** :23:35  
11/13/2016 **15** :23:35

---

<div class="post-metadata">

### Author: ![eliassal](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@eliassal](https://forums.powershell.org/u/eliassal)
#### Post date: [December 6, 2018, 10:50am UTC](https://forums.powershell.org/t/convert-french-date-to-us/11758/7 "2018-12-06T10:50:37Z")

</div>

Please ignore my last poste as the format specifier was 'u", I changed it to ‘O’ and all dates are correct now.

Thanks for your help

---

<div class="post-metadata">

### Author: ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)
#### Post date: [December 6, 2018, 11:05am UTC](https://forums.powershell.org/t/convert-french-date-to-us/11758/8 "2018-12-06T11:05:06Z")

</div>

Rather than messing with Culture, you can just tell ParseExact how to parse the date:

```
$date = '13/02/2017 10:04:07'

[datetime]::ParseExact($date, "dd/MM/yyyy H:mm:ss", [cultureinfo]::InvariantCulture)
#or
[datetime]::ParseExact($date, "dd/MM/yyyy H:mm:ss", $null)
```

Output:

```
PS C:\Users\Rob> $date = '13/02/2017 10:04:07'

[datetime]::ParseExact($date, "dd/MM/yyyy H:mm:ss", [cultureinfo]::InvariantCulture)
#or
[datetime]::ParseExact($date, "dd/MM/yyyy H:mm:ss", $null)

Monday, February 13, 2017 10:04:07 AM
Monday, February 13, 2017 10:04:07 AM
```

---

<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:33pm UTC](https://forums.powershell.org/t/convert-french-date-to-us/11758/9 "2024-05-16T20:33:38Z")

</div>


