Change Format Date String to DateFormat dd/MM/yyyy

Welcome Forums General PowerShell Q&A Change Format Date String to DateFormat dd/MM/yyyy

Viewing 4 reply threads
  • Author
    Posts
    • #191704
      Participant
      Topics: 1
      Replies: 3
      Points: -2
      Rank: Member

      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.

       

    • #191728
      Participant
      Topics: 0
      Replies: 42
      Points: 414
      Team MemberHelping Hand
      Rank: Contributor

      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
    • #191731
      Participant
      Topics: 1
      Replies: 3
      Points: -2
      Rank: Member

      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

      Thanks for replying

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

      thankyou

      • #191746
        Participant
        Topics: 0
        Replies: 28
        Points: 144
        Helping Hand
        Rank: Participant

        If you are looking for changing it in the existing file, you may try below code.

        $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
        
        

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

    • #191866
      Participant
      Topics: 1
      Replies: 3
      Points: -2
      Rank: Member

      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.

      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

       

    • #191887
      Participant
      Topics: 1
      Replies: 3
      Points: -2
      Rank: Member

      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.

      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

      PowerShell
      85 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 17.6px; width: 6.59775px; left: 51px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      #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
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      Case close.. thank for helping me.

      Have nice day

Viewing 4 reply threads
  • The topic ‘Change Format Date String to DateFormat dd/MM/yyyy’ is closed to new replies.