DateTimePicker Custom Format doesn't work

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
sebatozqc
Posts: 5
Joined: Fri Nov 13, 2020 10:06 am

DateTimePicker Custom Format doesn't work

Post by sebatozqc »

Hi guys,

I m trying to use custom format in dateTimePicker like this
$dateTimePicker.Format = DateTimePickerFormat.Custom
$dateTimePicker.CustomFormat = "dd MM yyyy"

But i always get this error :
DateTimePickerFormat.Custom : The term 'DateTimePickerFormat.Custom' is not recognized as the name of a cmdlet

Does anyone can help please.
Thx in advance.

Seb

User avatar
brittneyr
Site Admin
Posts: 645
Joined: Thu Jun 01, 2017 7:20 am
Answers: 8
Been upvoted: 6 times

Re: DateTimePicker Custom Format doesn't work

Post by brittneyr »

Update your format line to look like the following:

Code: Select all

$dateTimePicker.Format = [Windows.Forms.DateTimePickerFormat]::Custom
Brittney Ryn
SAPIEN Technologies, Inc.

sebatozqc
Posts: 5
Joined: Fri Nov 13, 2020 10:06 am

Re: DateTimePicker Custom Format doesn't work

Post by sebatozqc »

Yesss pretty cool. That works.
But after that, when i use the variable $dateTimePicker the result is : System.Windows.Forms.DateTimePicker, Value: 11/13/2020 2:16 PM
I need to have just 11/13/2020
How can i do that please ?

sebatozqc
Posts: 5
Joined: Fri Nov 13, 2020 10:06 am

Re: DateTimePicker Custom Format doesn't work

Post by sebatozqc »

To use with Set-ADAccountExpiration -Identity $identity -DateTime $dateTimePicker

sebatozqc
Posts: 5
Joined: Fri Nov 13, 2020 10:06 am

Re: DateTimePicker Custom Format doesn't work

Post by sebatozqc »

When i use it like this it give me this error :
Set-ADAccountExpiration : Cannot bind parameter 'DateTime'. Cannot convert the "System.Windows.Forms.DateTimePicker,
Value: 11/20/2020 2:22:58 PM" value of type "System.Windows.Forms.DateTimePicker" to type
"System.Nullable`1[System.DateTime]"

User avatar
brittneyr
Site Admin
Posts: 645
Joined: Thu Jun 01, 2017 7:20 am
Answers: 8
Been upvoted: 6 times

Re: DateTimePicker Custom Format doesn't work

Post by brittneyr »

That is because you are passing the DateTimePicker control, not a DateTime object. To get the DateTime object from your object, you would pass something like this:

Code: Select all

$dateTimePicker.Value
Brittney Ryn
SAPIEN Technologies, Inc.

sebatozqc
Posts: 5
Joined: Fri Nov 13, 2020 10:06 am

Re: DateTimePicker Custom Format doesn't work

Post by sebatozqc »

Yeah. A big thanks. You change my day :-)

Locked