简体   繁体   English

将字符串转换为Office365的特定格式的System.DateTime

[英]Converting String to System.DateTime to specific format for Office365

I'm running into an issue with converting user input into a DateTime type while returning it in a specific format. 我遇到了将用户输入转换为DateTime类型,同时以特定格式返回的问题。 I've been searching for a few hours now and everything I've tried so far hasn't worked. 我已经搜索了几个小时,到目前为止我尝试过的所有方法都无效。

When I enter the date, I want it to stay as the format I entered it in so that Get-MessageTrace can read it. 输入日期时,我希望它保持输入时的格式,以便Get-MessageTrace可以读取它。

What I'm looking for is to enter "1/23/2019" and ParseExact would shoot it back out as "1/23/2019". 我正在寻找的是输入“ 1/23/2019”, ParseExact会将其射回为“ 1/23/2019”。

What I'm getting is $NewDate1 being "Wednesday, January 23, 2019 12:01:00 AM" I'm probably not doing this right or maybe doing something wrong, but I could use some help, please. 我得到的是$NewDate1是“ Wednesday,January $NewDate1 AM”我可能没有正确执行此操作或做错了某些操作,但是请给我一些帮助。

$UserEmail = Read-Host -Prompt 'Enter your WT email address'
$Date1 = Read-Host -Prompt 'Enter the first date (MM/DD/YYYY)'
$Date2 = (Read-Host -Prompt 'Enter the second date (MM/DD/YYYY)'


$NewDate1 = [datetime]::parseexact($Date1, 'm/d/yyyy', $null)
$NewDate2 = [datetime]::parseexact($Date2, 'm/d/yyyy', $null) 


Get-MessageTrace -RecipientAddress 'UserEmail' -StartDate '$NewDate1'
-EndDate 'NewDate2'

Error I receive: 我收到的错误:

Cannot process argument transformation on parameter 'StartDate'. 无法处理参数“ StartDate”上的参数转换。 Cannot convert value "$NewDate1" to type "System.DateTime". 无法将值“ $ NewDate1”转换为类型“ System.DateTime”。 Error: "String was not recognized as a valid DateTime." 错误:“字符串未被识别为有效的DateTime。” + CategoryInfo : InvalidData: (:) [Get-MessageTrace], ParameterBindin...mationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-MessageTrace + PSComputerName : outlook.office365.com + CategoryInfo:InvalidData:(:) [Get-MessageTrace],ParameterBindin ... mationException + FullyQualifiedErrorId:ParameterArgumentTransformationError,Get-MessageTrace + PSComputerName:outlook.office365.com

First, regarding date string formatting, a lower case 'm' represents minutes and an uppercase 'M' represents months. 首先,关于日期字符串格式,小写的“ m”代表分钟,大写的“ M”代表月份。 That is why '1/23/2019' is being interpreted as Wednesday, January 23, 2019 12: 01 :00 AM (emphasis on first minute of the day). 这就是为什么“2019年1月23日”被解释为周三,2019年1月23日12:01:00 AM(强调当天的第一分钟)。 The pattern you likely want is 'M/d/yyyy'. 您可能想要的模式是“ M / d / yyyy”。

Second, I'm not entirely sure you understand date parsing. 其次,我不确定您是否理解日期解析。 ParseExact does not take a string and "shoot it back" as a date with a specific string representation. ParseExact不会接受字符串,而是使用特定的字符串表示形式将其“ ParseExact ”为日期。 ParseExact will convert a string formatted as a date into a DateTime object. ParseExact会将格式为日期的字符串转换为DateTime对象。 That DateTime object does not have an inherent format or string representation (unless you consider ToString() to be the canonical date format), it's just a date. DateTime对象没有固有的格式或字符串表示形式(除非您认为ToString()是规范的日期格式),它只是一个日期。 If, at a later time, you need a string representation of that date you will have to convert it to a string. 如果以后需要该日期的字符串表示形式,则必须将其转换为字符串。

Third, you are passing a string with the value of "$NewDate1" to -StartDate , hence the exception stating: 第三,您将一个值为“ $ NewDate1”的字符串传递给-StartDate ,因此出现异常:

Cannot convert value "$NewDate1" to type "System.DateTime" 无法将值“ $ NewDate1”转换为类型“ System.DateTime”

Remove the single quotes around '$NewDate1' to pass the value of the variable rather than the name of the variable. 删除'$ NewDate1'周围的单引号以传递变量的值而不是变量的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM