Date format month day year

is there a setting for the date format that will use month day year. I am using the new-udinputfield -type date but every time it uses day month year.
any assistance is appreciated
pg
New-UDInput -Content {

    New-UDInputField -Type "date" -Name "Datep" -Placeholder "Date"
} -Endpoint {
    param($Datep)

    #$volHoursEarned = 0 + $volHoursEarned
    $datep = Get-Date -Day $datep[1] -Month $datep[0] -Year $datep[2]

}

Hi @patg thanks for posting your questionā€¦Re-reading your question before I post this answer I would do something like:-

$newstartdate = $Datep.ToString() -replace ā€œ([0-9]+)/+([0-9]+)/+([0-9]+)ā€, ā€˜$2-$1-$3ā€™

This will give you the date in MM-DD-YYYY format

Thanks for the reply (I appreciate that!) - I tried a few things and cant seem to get it to do anything to the $datep ā€¦I think the formatting is coming from -type ā€˜dateā€™ but I really donā€™t know. I tried several variations of your suggestions with no luck. but thank you - I am going to bang my head some more donā€™t want to go back to powershell studio. I have my own form that can select a date but vs code doesnā€™t support winforms assembly. thanks for the ideas and insight though
New-UDInput -Content {

    New-UDInputField -type 'date' -Name "Datep" -Placeholder "Date"
} -Endpoint {
    param($Datep)
    #New-UDInputAction -toast $datep -duration 1000
    $newstartdate = $Datep.ToString() -replace ā€œ([0-9]+)/+([0-9]+)/+([0-9]+)ā€, ā€˜$2-$1-$3ā€™
    New-UDInputAction -Toast $newstartdate -duration 2000
  # $Datep.ToString() -replace ā€œ([0-9]+)/+([0-9]+)/+([0-9]+)ā€, ā€˜$2-$1-$3ā€™

}

What about this ?

$newstartdate = $Datep.ToString('dd-MM-yyyy')
New-UDInputAction -Toast $newstartdate -duration 2000

@psDevUK

.net ToString operator already support date formatting.
See : DateTime.ToString documentation for all the possibilities.

This is way better, easier and most importantly robust than using the replace operator since the default dateTime.Tostring() initial output can change depending on the system DateTime settings.

@itfranck I would normally go with this method, but for some reason this throws an errorā€¦so yesterdays suggestion was taken from a powershell studio script I got, and I didnā€™t actually test it. Sorry @patg I didnā€™t mean to throw any false leads. So anyway been thinking about this, this morning and I have the following working solution:-
Formatting your date
I actually tested this time in a dashboard, as mentioned I have used @itfranck way but had to get-date on the parameter before this would work else throws an error.

Thanks psDevUK and ITFranck ā€¦unfortunately itā€™s still not working.
what I noticed is that when I run the ā€˜Formatting your dateā€™ code (as supplied above - thanks for that), I get the result for the date july 1, 2019 as 01-07-2019 but when I change the $dateformat = $var.tostring(ā€˜dd-MM-yyyyā€™) I get 07-01-2019. that seems fine but if I choose date july 27, 2019 it errors out with cannot convert value"27-07-2019" to type ā€œsystem date timeā€ error ā€œ27-07-2019ā€ was not recognized as a valid date time.
I am not sure if itā€™s because my get-culture is en-US. not sure if I can change my get-culture to UK or something and see if that works.
thanks for any info and the help you already gave - appreciated.
pg

I did use the method from the feb 27 ā€˜datepicker formatā€™ forum post by Jacob-evansā€¦
it seemed no matter what config we used the -type ā€˜dateā€™ is set with a format that will not change (as far as I know - which isnā€™t far at all ;-)). so I just took the variable $datep and then broke it up into partsā€¦not pretty but hopefully this will do the job. itā€™s just the users will question why it shows initially on the form day month year but will be inserted into the db as month day yearā€¦

import-module -name UniversalDashboard
Get-UDDashboard | Stop-UDDashboard
$testpage = New-UDPage -Name test -Content {

New-UDInput -Content {

New-UDInputField -type 'date' -Name "Datep" -Placeholder "Date"

} -Endpoint {
param($Datep)
$d1 = $datep.split("-")
$d2 = $d1[1]+ā€™-ā€™+$d1[0]+ā€™-ā€™+$d1[2]
new-UDInputAction -Toast $d2 -duration 3000
}
}

$Dashboard = New-UDDashboard -Title ā€œDate Formattingā€ -Pages @(
$testpage
)
Start-UDDashboard -Dashboard $Dashboard -Port 8094 #-AutoReload

I digged a little bit more into it.
My solution didnā€™t quite work since $Datep is a string so I canā€™t use the DateTime.ToString method.

There is 2 thing to consider here :

  • The datetime parameter is returned as a string
  • The UDInput field will always return the following format dd-MM-yyyy (see below why)

Once we know that, you can parse the string to a date then format it as a string again.

$Datep = [DateTime]::ParseExact($Datep, 'dd-MM-yyyy', 
[System.Globalization.CultureInfo]::CurrentCulture).ToString('MM-dd-yyyy')

Or

$Datepsplit = $Datep.Split('-')
$Datep = "{0}-{1}-{2}" -f $Datepsplit[1],$Datepsplit[0],$Datepsplit[2]

As for the output format, this is hardcoded in the Ud-input-field.jsx React component behind the scenes
As you can see, it is hardcoded to use DD-MM-YYYY and do not make use of system culture or anything else.

edit :
I opened an enhancement request related to this.

1 Like

Damn youā€™re good ITFranck! both methods worked - much obliged. i thank you for the time you put in.

who put that format in the .jsx file! lol. killin me.

thanks again

1 Like

Thank you itfranck - job well doneā€¦I swim on the surface you swim at depths that are near unfathomable to me!
thanks

Glad you got this working @patg not sure why

    param($Datep)
    $var = get-date $Datep
    $DateFormat = $var.ToString('MM-dd-yyyy')
    New-UDInputAction -Toast $DateFormat -duration 2000

didnā€™t work because $var is a system.datetime object and you specify the output as @itfranck originally suggested. Take my hat off to the solution that was provided and for the reason behind why it is always formatted as dd-mm-yyyy in the ud-input-field.jsx file learn something new everyday as they say. :smile:

1 Like

Oh ! A lot simple to just use the Powershell Get-Date than going .net for the date conversion. I never used that cmdlet to parse an existing date before. Great !

This method has been working for me from 2.3.1 to 2.5.3.


          if ([string]::IsNullOrEmpty($FromDate)) {
            $StartDate = $Null;
          } else {
            ($dd,$mm,$year) = $FromDate.Split("-");
            $StartDate = $year + "-" + $mm + "-" + $dd;
          }```