Solved

convert string to datetime

Posted on 2011-09-16
2
1,537 Views
Last Modified: 2012-05-12
I am trying to import a csv file and convert the "reporteddate' column data to datatime and store back into a variable. I cannot get the new reportedconvertedtime filed to convert to datatime 9 outputs nothing )  Here is the script
$a = import-csv "C:\Documents and Settings\usr\My Documents\WindowsPowerShell\augmetrics.csv"
#convert string time to datetime
$newfile = $a| select Site,IncidentID, @{ name = 'ReportedConvertedTime' ; expression = { [datetime]::ParseExact($_.ReportedDate,"MM/dd/yyyy hh:mm:ss",$null)}} , 

Open in new window

Here is the member information for $a
   TypeName: System.Management.Automation.PSCustomObject

Name                    MemberType   Definition                                                                                                          
----                    ----------   ----------                                                                                                          
Equals                  Method       bool Equals(System.Object obj)                                                                                      
GetHashCode             Method       int GetHashCode()                                                                                                   
GetType                 Method       type GetType()                                                                                                      
ToString                Method       string ToString()                                                                                                   
Assignee                NoteProperty System.String Assignee=Company Tier 1                                                                             
Category                NoteProperty System.String Category=Medium                                                                                       
ClosedDate              NoteProperty System.String ClosedDate=8/2/2010 9:53:17 PM                                                                        
Email                   NoteProperty System.String Email=                                                                                                
FirstName               NoteProperty System.String FirstName=name                                                                                    
IncidentID              NoteProperty System.String IncidentID=INC000000024496                                                                            
LastModifiedBy          NoteProperty System.String LastModifiedBy=8/2/2010 9:53:17 PM                                                                    
LastModifiedDate        NoteProperty System.String LastModifiedDate=                                                                                     
LastName                NoteProperty System.String LastName=lastname                                                                                   
Notes                   NoteProperty System.String Notes=Dear Support,    I have come across this issue and believe it would be because of a permissio...
OperationCatTier1       NoteProperty System.String OperationCatTier1=                                                                                    
OperationCatTier2       NoteProperty System.String OperationCatTier2=                                                                                    
OperationCatTier3       NoteProperty System.String OperationCatTier3=                                                                                    
ProductCatTier1         NoteProperty System.String ProductCatTier1=                                                                                      
ProductCatTier2         NoteProperty System.String ProductCatTier2=                                                                                      
ProductCatTier3         NoteProperty System.String ProductCatTier3=                                                                                      
ReportedDate            NoteProperty System.String ReportedDate=8/1/2010 10:41:14 PM                                                                     
ReportedSource          NoteProperty System.String ReportedSource=emaili@email.com                                                         
ResolutionClosureSource NoteProperty System.String ResolutionClosureSource=From:  Support [mailto:support@email.com]   Sent: Sunday, Augu...
ResolutionMethod        NoteProperty System.String ResolutionMethod=                                                                                     
ServiceType             NoteProperty System.String ServiceType=User Service Restoration                                                                  
Site                    NoteProperty System.String Site=Marketing Office -                                                               
Status                  NoteProperty System.String Status=Closed                                                                                         
StatusReasonHidden      NoteProperty System.String StatusReasonHidden=                                                                                   
Submitter               NoteProperty System.String Submitter=mardinig                                                                                    
Summary                 NoteProperty System.String Summary=Marketing Office -  PLEASE ASSIST WITH ACCESS - INFANT CHARGES  screenshot attached

Open in new window

Here is the $newfile members
    TypeName: Selected.System.Management.Automation.PSCustomObject

Name                  MemberType   Definition                                    
----                  ----------   ----------                                    
Equals                Method       bool Equals(System.Object obj)                
GetHashCode           Method       int GetHashCode()                             
GetType               Method       type GetType()                                
ToString              Method       string ToString()                             
IncidentID            NoteProperty System.String IncidentID=INC000000024496      
ReportedConvertedTime NoteProperty  ReportedConvertedTime=null                   
Site                  NoteProperty System.String Site=Marketing Office 

Open in new window

0
Comment
Question by:davesnb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
Dale Harris earned 500 total points
ID: 36552849
Try this instead:

$a = import-csv "C:\Documents and Settings\usr\My Documents\WindowsPowerShell\augmetrics.csv"
#convert string time to datetime
$newfile = $a| select Site,IncidentID, @{ name = 'ReportedConvertedTime' ; expression = { $([datetime]$_.ReportedDate)}}

Not sure if I have enough Parentheses in there as it's more important you try to convert the existing string to datetime.

$DateTime = [datetime]"8/1/2010 10:41:14 PM"

Above is the attempt we are doing.

If your actual string is "ReportedDate=8/1/2010 10:41:14 PM" then we can just do a simple string parse.

HTH,

Dale Harris
0
 

Author Closing Comment

by:davesnb
ID: 36554160
That worked out great, thanks.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question