Change datetimepicker value to the 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 9 years and 8 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
adiagne
Posts: 3
Last visit: Thu Sep 17, 2015 6:39 am

Change datetimepicker value to the current date

Post by adiagne »

Hi everybody,

I'm here since I have a little problem with the Datetimepicker Form.
I use PowerShell Studio 2014 and I can't fix the default value of an Datetimepicker object named "expirationDate" to the current date.
I've tried to set this one with these commands :

1) $expirationDate.Value.Date = [system.datetime](Get-Date -format )
2) $expirationDate.Value = Get-Date -format g
3) $expirationDate.Value = [system.datetime](Get-Date -format g)

But none match.
On the Designer of Powershell Studio, when the Datetimepicker object is selected, I can't choose a dynamic variable "current date" on the parameter "value". I don't know how to do that. I've searched a long time on the internet but nobody talk about that.

Did someone have a solution for this issue? :roll:


Best regards :) .
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Change datetimepicker value to the current date

Post by jvierra »

It is a date not a str4ing.

$expirationDate.Value=[system.datetime]::Now
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Change datetimepicker value to the current date

Post by jvierra »

In case you don't realize this the default value of the DTP is todays date.

If you want to reset to the original date then it takes a couple of more steps.
User avatar
adiagne
Posts: 3
Last visit: Thu Sep 17, 2015 6:39 am

Re: Change datetimepicker value to the current date

Post by adiagne »

jvierra wrote:It is a date not a str4ing.

$expirationDate.Value=[system.datetime]::Now

Thanks you ! I have read the Get-Date cmdlet library and I haven't seen before that it return a datetime value when we just use get-date cmd and a string when we add a format argument to this one. :oops:

I think that I have founded the solution for my problem. When we use a custom format for DTP the default value isn't today with powershell studio GUI. I think that it was my problem. Now it works by clearing the custom format.

Thanks for your answer,
best regards.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Change datetimepicker value to the current date

Post by jvierra »

When you format a date it returns a string. When you just ask it for a date you get a date object.

If you want to display a special date format then use the datepicker format property to set the format. Do not use get-Date.

Always inspect the object type and look at the property types when using PowerSHell.

$datepicker1.Value.GetType()
(Get-Date).GetType()
(Get-Date -format g).GetType()



Now you can see what matches and what doesn't.
User avatar
adiagne
Posts: 3
Last visit: Thu Sep 17, 2015 6:39 am

Re: Change datetimepicker value to the current date

Post by adiagne »

jvierra wrote:When you format a date it returns a string. When you just ask it for a date you get a date object.

If you want to display a special date format then use the datepicker format property to set the format. Do not use get-Date.

Always inspect the object type and look at the property types when using PowerSHell.

$datepicker1.Value.GetType()
(Get-Date).GetType()
(Get-Date -format g).GetType()



Now you can see what matches and what doesn't.

Thanks you so much for your help ! :)

Best regards !
This topic is 9 years and 8 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