Change Format Date String to DateFormat dd/MM/yyyy

Dear All,

I am new in Powershell.
I have exported file to csv

"EMPNO","DATE","TIME"
"19079","12/2/2019 12:00:00 AM","17:30"
"2121926","12/2/2019 12:00:00 AM","17:36"
"8011908","12/2/2019 12:00:00 AM","18:06"
"59055","12/2/2019 12:00:00 AM","18:07"

I wanna change date format to dd/MM/yyyy only. Can i get sample code to resolve this ?

Thanks before.

 

Depends on how you’re exporting, but as an example:

PS C:\..\james.ruskin>"12/2/2019 12:00:00 AM" | Get-Date -Format "dd/MM/yyyy"
12/02/2019

[quote quote=191728]Depends on how you’re exporting, but as an example:

<textarea class="ace_text-input" style="opacity: 0; height: 17.6px; width: 6.59775px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
PS C:\..\james.ruskin>"12/2/2019 12:00:00 AM" | Get-Date -Format "dd/MM/yyyy"
12/02/2019
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]

Thanks for replying

That’s on csv file, am i must change date format one by one ?

thankyou

If you are looking for changing it in the existing file, you may try below code.
[pre]
$CSV = Import-Csv “C:\Scripts\ps.org\test.csv”
$CSV | % {
$PSItem.DATE = Get-Date $PSItem.DATE -Format ‘dd/MM/yyyy’
}

$CSV | Export-Csv -Path “C:\Scripts\ps.org\test_new.csv” -NoTypeInformation

[/pre]

At the same time, you could the same formatting while exporting to CSV file.

[quote quote=191746]If you are looking for changing it in the existing file, you may try below code.

PowerShell
8 lines
<textarea class="ace_text-input" style="opacity: 0; height: 17.6px; width: 6.59775px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
8
$CSV = Import-Csv "C:\Scripts\ps.org\test.csv"
$CSV | % {
$PSItem.DATE = Get-Date $PSItem.DATE -Format 'dd/MM/yyyy'
}
$CSV | Export-Csv -Path "C:\Scripts\ps.org\test_new.csv" -NoTypeInformation
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
At the same time, you could the same formatting while exporting to CSV file.

[/quote]

Hello rejikodiyil,

Thanks for reply and your code working fine. Thankyou very much.

And now i try to upload file to sftp (winscp) and wanna add timestamp into file.

e.g: Att.xls to Att_dd/MM/yyyy_HH:mm:ss.xls

below the script, but output file will be :

Att.xlsdd/MM/yyyy . Can you help? Thanks before

#Preparing log
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
#Log location file
Start-Transcript -path D:\Script\Log\Log.txt -append

try 
{ 
# Load WinSCP .NET assembly 
[Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\WinSCP\WinSCPnet.dll") | Out-Null 

# Set FTP data
$SessionOptions.DisableVersionCheck 
$sessionOptions = New-Object WinSCP.SessionOptions 
$sessionOptions.Protocol = [WinSCP.Protocol]::sftp 
$sessionOptions.HostName = "hostname" 
$sessionOptions.PortNumber = "xxxx"
$sessionOptions.UserName = "username" 
$sessionOptions.Password = "password" 
$sessionOptions.SshHostKeyFingerprint = "ssh-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 

$session = New-Object WinSCP.Session 


try 
{ 
# Connect 
$session.Open($sessionOptions) 

$localPath = "D:\Script\*.xls" 
$remotePath = "/" 
$backupPath = "D:\Script\Upload"
#$aData = Get-Date
$destPath =
$backupPath +
(Split-Path -Path $transfer.FileName -Leaf) +
(Get-Date -Format "yyyy-MM-dd HH:mm:ss") 
Move-Item $transfer.FileName $destPath




# Upload files, collect results 
#$TransferOptions() 

$transferOptions = New-Object WinSCP.TransferOptions 
$transferResult = $session.PutFiles($localPath, $remotePath) 

# Files itineration
foreach ($transfer in $transferResult.Transfers) 
{ 
# Correct o error? 
if ($transfer.Error -eq $Null) 
{ 
Write-Host ("${aData} - File {0} was uploaded and moved to backup folder" -f 
$transfer.FileName) 
# If, Ok move to backup folder
Move-Item $transfer.FileName $backupPath -force 
} 
else 
{ 
Write-Host ("${aData} - File {0} with error: {1}" -f 
$transfer.FileName, $transfer.Error.Message) 
} 
} 
} 

finally 
{ 
# Disconnect and clean
$session.Dispose() 
} 

exit 0 
} 

catch [Exception] 
{ 
Write-Host $_.Exception.Message 
exit 1 
} 

# Stop log writing
Stop-Transcript

 

Case close… thank for helping me.

Have nice day