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;
          }```