Change datetimepicker value to the current date

Ask your Windows 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.
Locked
User avatar
adiagne
Posts: 3
Joined: Tue Sep 17, 2013 5:11 am

Change datetimepicker value to the current date

Post by adiagne » Mon Jul 07, 2014 6:53 am

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 :) .

User avatar
jvierra
Posts: 13677
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Change datetimepicker value to the current date

Post by jvierra » Mon Jul 07, 2014 7:00 am

It is a date not a str4ing.

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

User avatar
jvierra
Posts: 13677
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Change datetimepicker value to the current date

Post by jvierra » Mon Jul 07, 2014 7:05 am

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
Joined: Tue Sep 17, 2013 5:11 am

Re: Change datetimepicker value to the current date

Post by adiagne » Tue Jul 08, 2014 2:38 am

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.

User avatar
jvierra
Posts: 13677
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Change datetimepicker value to the current date

Post by jvierra » Tue Jul 08, 2014 5:55 am

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
Joined: Tue Sep 17, 2013 5:11 am

Re: Change datetimepicker value to the current date

Post by adiagne » Wed Jul 09, 2014 3:31 am

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 !

Locked