Home > database >  Converting date string to DateTime (Aspose PDF Library)
Converting date string to DateTime (Aspose PDF Library)

Time:03-31

I'm using the Aspose library within PowerShell and having some issues with the CreationDate values that Aspose is giving me back.

$pdf = New-Object Aspose.Pdf.Facades.PdfFileInfo($pdffile)
$pdf.CreationDate

This returns a string with the value of:

D:20210630133144 10'00'

However PowerShell gives me this error when I try to cast it as DateTime

Cannot convert value "D:20210630133144 10'00'" to type "System.DateTime". Error: "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."

I even tried trimming the first 2 chars but then the " 10'00'" timezone at the end causes it to fail as well. I could trim this too, but PowerShell seems to be able to intepret this value as a date automatically when displaying all properties:

PowerShell command line interpreting string as date

I found a similar thread on the Aspose forums where it seems to work as expected in C#. What am I missing here?

EDIT: Output of $pdf | Get-Member

   TypeName: Aspose.Pdf.Facades.PdfFileInfo

Name                 MemberType Definition
----                 ---------- ----------
BindPdf              Method     void BindPdf(Aspose.Pdf.Document srcDoc), void BindPdf(string srcFile), void BindPdf...
ClearInfo            Method     void ClearInfo()
Close                Method     void Close(), void IFacade.Close()
Dispose              Method     void Dispose(), void IDisposable.Dispose()
Equals               Method     bool Equals(System.Object obj)
GetDocumentPrivilege Method     Aspose.Pdf.Facades.DocumentPrivilege GetDocumentPrivilege()
GetHashCode          Method     int GetHashCode()
GetMetaInfo          Method     string GetMetaInfo(string name)
GetPageHeight        Method     float GetPageHeight(int pageNum)
GetPageRotation      Method     int GetPageRotation(int pageNum)
GetPageWidth         Method     float GetPageWidth(int pageNum)
GetPageXOffset       Method     float GetPageXOffset(int pageNum)
GetPageYOffset       Method     float GetPageYOffset(int pageNum)
GetPdfVersion        Method     string GetPdfVersion()
GetType              Method     type GetType()
Save                 Method     void Save(System.IO.Stream destStream), void Save(string destFile), void ISaveableFa...
SaveNewInfo          Method     bool SaveNewInfo(System.IO.Stream outputStream), bool SaveNewInfo(string outputFile)
SaveNewInfoWithXmp   Method     bool SaveNewInfoWithXmp(string outputFileName)
SetMetaInfo          Method     void SetMetaInfo(string name, string value)
ToString             Method     string ToString()
Author               Property   string Author {get;set;}
CreationDate         Property   string CreationDate {get;set;}
Creator              Property   string Creator {get;set;}
Document             Property   Aspose.Pdf.Document Document {get;}
HasCollection        Property   bool HasCollection {get;}
HasEditPassword      Property   bool HasEditPassword {get;}
HasOpenPassword      Property   bool HasOpenPassword {get;}
Header               Property   System.Collections.Generic.Dictionary[string,string] Header {get;set;}
InputFile            Property   string InputFile {get;set;}
InputStream          Property   System.IO.Stream InputStream {get;set;}
IsEncrypted          Property   bool IsEncrypted {get;}
IsPdfFile            Property   bool IsPdfFile {get;}
Keywords             Property   string Keywords {get;set;}
ModDate              Property   string ModDate {get;set;}
NumberOfPages        Property   int NumberOfPages {get;}
PasswordType         Property   Aspose.Pdf.PasswordType PasswordType {get;}
Producer             Property   string Producer {get;}
Subject              Property   string Subject {get;set;}
Title                Property   string Title {get;set;}

CodePudding user response:

I personally have never seen this exact DateTime format before, there might be a way to parse it as is but here is an alternative that requires the trimming of D: and the removal of both appearances of ':

$date = "D:20210630133144 10'00'"

[datetime]::ParseExact(
    $date.Trim("D:").Replace("'",''),
    "yyyyMMddHHmmsszzzz",
    [cultureinfo]::InvariantCulture
)

# Results in:
# Wednesday, June 30, 2021 12:31:44 AM
  • Related