Solved

convert string to datetime

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

15 Experts available now in Live!

Get 1:1 Help Now