如何格式化日期值,其中结果包含多个日期字段。

huangapple go评论59阅读模式
英文:

Howto format datevalues where result contains several date fields

问题

以下是翻译好的部分:

'用户1','用户2' | foreach-object { 
    get-aduser -identity $_ -Properties * | 
    select name,DisplayName,samaccountname,enabled, accountexpirationdate,whenchanged }| ft

name   DisplayName  samaccountname enabled accountexpirationdate whenchanged        
----   -----------  -------------- ------- --------------------- -----------        
user1, 我的姓名,     user1          False 11-04-2023 00:00:00   11-04-2023 00:42:21
user2, 我的姓名,     user2          False 11-04-2023 00:00:00   11-04-2023 00:42:21

请告诉我您需要如何格式化日期字段为短日期格式。

英文:
'user1','user2' | foreach-object { 
    get-aduser -identity $_ -Properties * | 
    select name,DisplayName,samaccountname,enabled, accountexpirationdate,whenchanged }| ft


name   DisplayName  samaccountname enabled accountexpirationdate whenchanged        
----   -----------  -------------- ------- --------------------- -----------        
user1, MyName,       user1          False 11-04-2023 00:00:00   11-04-2023 00:42:21
user2, MyName,       user2          False 11-04-2023 00:00:00   11-04-2023 00:42:21

I want to have all the date fields formatted as shortdates

I have tried to add -format string to each datefield without success

答案1

得分: 1

这应该能满足你的需求

'user1','user2' | foreach-object { 
    get-aduser -identity $_ -Properties * | 
    select name,DisplayName,samaccountname,enabled,@{n='ExpirationDate';e={Get-Date $_.accountexpirationdate -Format 'dd-MM-yy'}},@{n='WhenChanged';e={Get-Date $_.whenchanged -Format 'dd-MM-yy'}} 
}| ft

在需要的地方更改每个条目中的格式位,以调整返回的确切日期格式。

英文:

This should do what you're looking for

'user1','user2' | foreach-object { 
    get-aduser -identity $_ -Properties * | 
    select name,DisplayName,samaccountname,enabled,@{n='ExpirationDate';e={Get-Date $_.accountexpirationdate -Format 'dd-MM-yy'}},@{n='WhenChanged';e={Get-Date $_.whenchanged -Format 'dd-MM-yy'}} 
}| ft

where you can tweak the exact date format being returned by changing the format bit in each

@{n='ExpirationDate';e={Get-Date $_.accountexpirationdate -Format 'dd-MM-yy'}}

entry as needed.

huangapple
  • 本文由 发表于 2023年5月17日 16:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定