``
Add-Type -TypeDefinition 'using System;
using System.Management.Automation;
public class FixedVariableNow : PSVariable
{public FixedVariableNow() : base("Now", 0, ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope)
{}
public override object Value
{get { return DateTime.Now; }}
}'
$ExecutionContext.SessionState.PSVariable.Set((New-Object -TypeName FixedVariableNow))
$Now.GetDateTimeFormats().count
Pause
$Now.GetDateTimeFormats()`
Run the code above on PowerShell V5.1 and PowerShell V7.1 and compare the output
On Powershell V5.1 115 items are returned.
on V7.1 only 30 items are returned.
Name Value
---- -----
PSVersion 7.1.0
PSEdition Core
GitCommitId 7.1.0
OS Microsoft Windows 10.0.19042
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Name Value
---- -----
PSVersion 5.1.19041.610
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.610
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
@RG255 Could you format the input code you post ?
Can you explain the scenario of this issue ? I mean what are the consequences in your code, what is the final problem in your development.
Can you explain the scenario of this issue? I mean what are the consequences in your code, what is the final problem in your development.
The code is part of a function in a module called in this way
Get-mydatetime -outputformat 78
the parameter is validated and the validation fails in v7.1 because the value 78 is out of range. to show the output
The last three lines are just there to show the output. The function just returns the format asked for.
The code should be readable in the original post
@RG255 first, the documentation about GetDateTimeFormats :
_You should not assume that multiple calls to the GetDateTimeFormats overloads will return identical data. Depending on the specific overload, the data returned by this method can change if the current culture changes, the user overrides individual cultural settings, or an update occurs to the system's cultural data._
Source : https://docs.microsoft.com/en-us/dotnet/api/system.datetime.getdatetimeformats?view=net-5.0
So the code you use is a bad pattern. Nothing can resolve it, you must move to a good pattern.
(Windows PowerShell is dot.net framework, PowerShell is dot.net core, if you use API directly, you have to manage the lifecycle of the API)
Get-Date should be enough powerfull to do what you need.
I will search later why the method GetDateTimeFormats of DateTime has so much difference between dotnet and dotnetcore but the code is not in this repo, so I don't think it could be resolved here.
Converts the value of this instance to all the string representations supported by the standard date and time format specifiers.
You could run ([datetime]::now.GetDateTimeFormats()).count and compare - it is a question for .Net Rumtime repository if you want in depth explanations.
OK I accept the above and have looked at the option @iSazonov suggested. I note the following:
There were some duplications in the list on V5.1 and when those are removed the number of items drops to 54. Looking at the differences between both outputs I see that the things that seem to be common are all the ones using the 24-hour clock. The ones that are missing are the ones that use the 12-hour clock (which add the "PM") and also the option to drop the leading zero.
V5.1 (54 items)
04:09 PM
04:09:09 PM
16:09
16:09:09
17 November
17 November 2020
17 November 2020 04:09 PM
17 November 2020 04:09:09 PM
17 November 2020 16:09
17 November 2020 16:09:09
17 November 2020 4:09 PM
17 November 2020 4:09:09 PM
17.11.20
17.11.20 04:09 PM
17.11.20 04:09:09 PM
17.11.20 16:09
17.11.20 16:09:09
17.11.20 4:09 PM
17.11.20 4:09:09 PM
17/11/20
17/11/20 04:09 PM
17/11/20 04:09:09 PM
17/11/20 16:09
17/11/20 16:09:09
17/11/20 4:09 PM
17/11/20 4:09:09 PM
17/11/2020
17/11/2020 04:09 PM
17/11/2020 04:09:09 PM
17/11/2020 16:09
17/11/2020 16:09:09
17/11/2020 4:09 PM
17/11/2020 4:09:09 PM
2020-11-17
2020-11-17 04:09 PM
2020-11-17 04:09:09 PM
2020-11-17 16:09
2020-11-17 16:09:09
2020-11-17 16:09:09Z
2020-11-17 4:09 PM
2020-11-17 4:09:09 PM
2020-11-17T16:09:09
2020-11-17T16:09:09.7056960+00:00
4:09 PM
4:09:09 PM
November 2020
Tue, 17 Nov 2020 16:09:09 GMT
Tuesday, 17 November 2020
Tuesday, 17 November 2020 04:09 PM
Tuesday, 17 November 2020 04:09:09 PM
Tuesday, 17 November 2020 16:09
Tuesday, 17 November 2020 16:09:09
Tuesday, 17 November 2020 4:09 PM
Tuesday, 17 November 2020 4:09:09 PM
V7.1 (20 Items)
16:06
16:06:58
17 Nov 2020
17 Nov 2020 16:06
17 Nov 2020 16:06:58
17 November
17 November 2020
17 November 2020 16:06
17 November 2020 16:06:58
17/11/2020
17/11/2020 16:06
17/11/2020 16:06:58
2020-11-17 16:06:58Z
2020-11-17T16:06:58
2020-11-17T16:06:58.0087641+00:00
November 2020
Tue, 17 Nov 2020 16:06:58 GMT
Tuesday, 17 November 2020
Tuesday, 17 November 2020 16:06
Tuesday, 17 November 2020 16:06:58
Given the option of using (as I have done above):
([datetime]::now.GetDateTimeFormats()) | sort -unique
which I assume will make use of the API as it may or may not change in the future.
I can adjust the code accordingly, but I can't get at the missing options this way.
OK I can use "get-date" but the formatting options are more difficult to use and this way made it easy.
Comments appreciated and I will look at tweaking the code I am using hoping the missing options will re-appear?
I think these are the formats that are missing:
04:11 PM
04:11:59 PM
17 November 2020 04:11 PM
17 November 2020 04:11:59 PM
17 November 2020 4:11 PM
17 November 2020 4:11:59 PM
17.11.20
17.11.20 04:11 PM
17.11.20 04:11:59 PM
17.11.20 16:11
17.11.20 16:11:59
17.11.20 4:11 PM
17.11.20 4:11:59 PM
17/11/20
17/11/20 04:11 PM
17/11/20 04:11:59 PM
17/11/20 16:11
17/11/20 16:11:59
17/11/20 4:11 PM
17/11/20 4:11:59 PM
17/11/2020 04:11 PM
17/11/2020 04:11:59 PM
17/11/2020 4:11 PM
17/11/2020 4:11:59 PM
2020-11-17
2020-11-17 04:11 PM
2020-11-17 04:11:59 PM
2020-11-17 16:11
2020-11-17 16:11:59
2020-11-17 4:11 PM
2020-11-17 4:11:59 PM
4:11 PM
4:11:59 PM
Tuesday, 17 November 2020 04:11 PM
Tuesday, 17 November 2020 04:11:59 PM
Tuesday, 17 November 2020 4:11 PM
Tuesday, 17 November 2020 4:11:59 PM
@RG255 I don't know that you want but DateTimeFormat comes from Cuture-s. .Net imports Culture-s from OS. It's not as easy as it sounds. You can find our discussion about adding a ListAvailable parameter to Get-Culture.
Also you can collect DateTimeFormat-s with (Get-Culture -ListAvailable).DateTimeFormat.
This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.
Most helpful comment
You could run
([datetime]::now.GetDateTimeFormats()).countand compare - it is a question for .Net Rumtime repository if you want in depth explanations.