Get-UDElement unable to get date or time values from New-UDDatePicker or New-UDTimePicker

Product: PowerShell Universal
Version: 4.1.21

I have spent quite some time on this. However, I don’t seem to find a way to retrieve the date or time value from either New-UDDatePicker or New-UDTimePicker.

I know that using onchange {} will have the value in the $EventData. But how can I get to those values later in the script. User need to be able to just set a time or a date and a time in a Show-UDModal. I will then extract the date part from the New-UDDatePicker and the Time part from the New-UDTimePicker. After stitching them back together, it will be inserted as a new datetime into a table. However, before I can do that, I need to be able to retrieve the 2 values. Is this a bug in 4.1.21, or how is this done?

If I test accessing the values in PowerShell, it works just fine.

PS C:> $TestDate = New-UDDatePicker -Id “Date-Test” -Format “DD.MM.YYYY” -Value (Get-Date)

PS C:> $TestDate.Value

Mittwoch, 30. Oktober 2024 10:36:07

Following is one of the many ways I have tried so far. Any help with this is much appreciated!

New-UDApp -Title ‘Test DateTime Picker’ -Content {
New-UDCard -Id “CD1” -Title “Set Date and Time” -Content {
New-UDLayout -Columns 1 -Content {

        New-UDButton -Id "btSetDateTime" -Text "Click to set Date & Time" -OnClick {

            Show-UDModal -Content {
                New-UDForm -Content {
                    New-UDLayout -Columns 2  -Content {
                        New-UDDatePicker -Id "DP1" -Label "Set End Date" -Value (Get-Date) -Format "DD.MM.YYYY"
                        New-UDTimePicker -Id "TP1" -Label "Set End Time" -Value ((Get-Date).AddHours(1)) -DisableAmPm
                    }
                    $Session:DP1date = (Get-UDElement -Id "DP1").value
                    $Session:TP1time = (Get-UDElement -Id "TP1").value
                    $Session:DP1TP1datetime = "$($Session:DP1date) $($Session:TP1time)"
                } -OnSubmit {
                    Show-UDToast -Message " Date & Time: '$($Session:DP1TP1datetime)'" -Duration 2000 -Position topCenter
                    Set-UDElement -Id "tbDateTime" -Attributes @{Value = "$($Session:DP1TP1datetime)"}
                    Hide-UDModal
                } -OnCancel {
                    Show-UDToast -Message "Cancelled..."
                    Hide-UDModal
                }
            }

        }
        New-UDTextbox -Id "tbDateTime" -Value "Not set yet"

    }
}

}

You already use session variables to store the data.
So why not do that in -onSubmit and get the data from $eventdata

At least in v5 that would work.


New-UDCard -Id “CD1” -Title “Set Date and Time” -Content {
    New-UDLayout -Columns 1 -Content {

        New-UDButton -Id "btSetDateTime" -Text "Click to set Date & Time" -OnClick {

            Show-UDModal -Content {
                New-UDForm -Content {
                    New-UDLayout -Columns 2  -Content {
                        New-UDDatePicker -Id "DP1" -Label "Set End Date" -Value (Get-Date) -Format "DD.MM.YYYY"
                        New-UDTimePicker -Id "TP1" -Label "Set End Time" -Value ((Get-Date).AddHours(1)) -DisableAmPm
                    }

                } -OnSubmit {
                    $Session:DP1date = $($eventdata.DP1)
                    $Session:TP1time = $($eventdata.TP1)
                    $Session:DP1TP1datetime = "$($Session:DP1date) $($Session:TP1time)"
                    Show-UDToast -Message " Date & Time: '$($Session:DP1TP1datetime)'" -Duration 2000 -Position topCenter
                    Set-UDElement -Id "tbDateTime" -Attributes @{Value = "$($Session:DP1TP1datetime)"}
                    Hide-UDModal
                } -OnCancel {
                    Show-UDToast -Message "Cancelled..."
                    Hide-UDModal
                }
            }

        }
        New-UDTextbox -Id "tbDateTime" -Value "Not set yet" -FullWidth

    }
}```

Hello @deroppi ,
This also works in 4.2.21. Thanks a lot for the solution. I was not aware that the values of those components can be accessed via $EventData.[Component Id]. Awesome to know!

I have just added a bit of date/string fiddling to assemble both dates back into one. It seems a bit odd that there is no built in DateTime Picker. However, this does the trick! Cheers!

New-UDApp -Title 'Test DateTime Picker' -Content {
    New-UDCard -Id "CD1" -Title "Set Date and Time" -Content {
        New-UDLayout -Columns 1 -Content {
            New-UDButton -Id "btSetDateTime" -Text "Click to set Date & Time" -OnClick {
                Set-UDElement -Id "tbDateTime" -Attributes @{Value = "Not set yet"}
                Show-UDModal -Content {
                    New-UDForm -Content {
                        New-UDLayout -Columns 2  -Content {
                            New-UDDatePicker -Id "DP1" -Label "Set End Date" -Value (Get-Date) -Format "DD.MM.YYYY"
                            New-UDTimePicker -Id "TP1" -Label "Set End Time" -Value ((Get-Date).AddHours(1)) -DisableAmPm
                        }
                    } -OnSubmit {
                        $DP1date = (Get-Date $eventdata.DP1 -Format "dd.MM.yyyy").ToString()
                        Show-UDToast -Message "Date : '$($DP1date)'" -Duration 2000 -Position topCenter
                        $TP1time = (Get-Date $eventdata.TP1 -Format "HH:mm").ToString()
                        Show-UDToast -Message " Date & Time: '$($TP1time)'" -Duration 2000 -Position topCenter
                        $Session:DP1TP1datetime = Get-Date "$DP1date $TP1time" -Format "dd.MM.yyyy HH:mm"

                        Show-UDToast -Message "Date & Time: '$($Session:DP1TP1datetime)'" -Duration 2000 -Position topCenter
                        Set-UDElement -Id "tbDateTime" -Attributes @{Value = "$($Session:DP1TP1datetime)"}
                        Hide-UDModal
                    } -OnCancel {
                        Show-UDToast -Message "Cancelled..."
                        Hide-UDModal
                    }
                }
            }
            New-UDTextbox -Id "tbDateTime" -Value "Not set yet" -FullWidth
        }
    }
}
1 Like