Created
June 29, 2020 21:06
-
-
Save DBremen/a328c26d4461c747a95fedfb9cc3bc56 to your computer and use it in GitHub Desktop.
PowerShell custom ArgumentTransformation attribute for DateTime example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define a format that is supported by our attribute | |
$stringDate = '4/14 4:12PM' | |
# try it first with the normal datetime type | |
[datetime]'4/14 4:12PM' | |
# this threw an error | |
# define a variable that utilizes the attribute | |
[datetime][DateTransform()]$dt = $stringDate | |
# no error | |
$dt | |
# now let's add another format string utilizing the parameter of our custom attribute | |
# note that the parameter accepts an array arguments. The argument(s) need to be always put in parenthesis for an array argument. | |
[DateTransformAttribute(('xxx dd.MM hh:mm'))]$dt2 = 'xxx 15.12 10:45' | |
$dt2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment