Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

convert string to datetime

Posted on 2011-09-16
2
Medium Priority
?
1,586 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 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