Solved

convert string to datetime

Posted on 2011-09-16
2
1,521 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
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This article will help you understand what HashTables are and how to use them in PowerShell.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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