将日期字符串转换为日期时间(Aspose PDF 库)

Converting date string to DateTime (Aspose PDF Library)

我在 PowerShell 中使用 Aspose 库,但 Aspose 返回给我的 CreationDate 值有一些问题。

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

这个returns一个字符串,值为:

D:20210630133144+10'00'

但是,当我尝试将其转换为 DateTime 时,PowerShell 给了我这个错误

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."

我什至尝试 trim输入前 2 个字符,但最后的“+10'00'”时区也导致它失败。我也可以 trim 这个,但是 PowerShell 似乎能够在显示所有属性时自动将此值解释为日期:

PowerShell command line interpreting string as date

我在 Aspose 论坛上发现了一个类似的 thread,它似乎在 C# 中按预期工作。我在这里错过了什么?

编辑:$pdf 的输出 |获取成员

   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;}

我个人以前从未见过这种确切的 DateTime 格式,可能有一种方法可以按原样 解析它 但这里有一个需要修剪的替代方法D: 并删除了 ':

的两次出现
$date = "D:20210630133144+10'00'"

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

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