Solved

convert string to datetime

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now