Set DateTimePicker to current date

Ask your PowerShell-related questions, including questions on cmdlet development!
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.
This topic is 8 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
kungfujedis
Posts: 1
Last visit: Thu Nov 30, 2017 1:53 pm

Set DateTimePicker to current date

Post by kungfujedis »

I want the date time picker to have the current date selected when the script is run. Currently, it always shows the date time of the last time i edited the script (aka, clicked "now" in the designer). I found this post: viewtopic.php?t=7837 ; in which it says that default behavior is already to show the current date. However, this is not what is happening in my script. I've tried setting the values at the beginning of my script with $datetimepicker.value = get-date -format d, or $datetimepicker.value = [system.datetime]::Now and other variations, but the date displayed in the form GUI does not seem to change. Suggestions?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Set DateTimePicker to current date

Post by jvierra »

Don't assign a value. It will show current date by default. If you set a date accidentlly hen delete the control and add it again. Do not set the date. The new control will work.

As an Alternate reset the date in the form load event.

$datetimepicker1.Value = [datetime]::Now
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Set DateTimePicker to current date

Post by dan.potter »

Set the custom format in the designer. I use this when I call the form as I'm not concerned with past dates.

$form1_Load = {

$dateTimePicker1.MinDate = Get-Date

}
This topic is 8 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked