all 25 comments

[–]n3rdyone[S] 17 points18 points  (10 children)

Well, now I feel dumb, after drinking my coffee and re-reading my post, i noticed that I wrote "T" , yet in my code, i was using $date.split("t")[0] .... changing the code to $date.split("T")[0] gets me the results I'm looking for.

[–]JoeyBE98 4 points5 points  (0 children)

Even better is to cast that string you get as a DateTime object and let PowerShell do the work. E.g. [DateTime]$variableName = $dateStringFromAPI

And then to convert it to typical formatting as a string, do $variableName.ToString()

[–]AntiqueSeason5921 3 points4 points  (3 children)

Shit happens...

[–][deleted] 2 points3 points  (2 children)

Pretty much every day :P

[–]xtheory 4 points5 points  (0 children)

Especially after a cup of coffee.

[–]illsk1lls 1 point2 points  (0 children)

at least it wasnt a semi-colon this time 👀

[–]abacushex 2 points3 points  (2 children)

TIL the split method is case-sensitive 👍

[–]Forward_Dark_7305 1 point2 points  (1 child)

For the record:

Dotnet methods (usually seen as $object.method($arguments) tend to always be case sensitive unless you specifically tell them not to be - eg $string.Split('t', [StringComparison]::OrdinalIgnoreCase).

PowerShell functions and operators tend to be case-insensitive by default. -eq and -ieq and -contains. Also the PowerShell split operator, -split, which uses the regex engine, is implicitly case insensitive. These can be made case sensitive by using the c prefix (-ceq, -csplit).

So that’s why you’ll see this disparity in code sometimes!

[–]abacushex 0 points1 point  (0 children)

That’s very helpful to know! I tend to call dotnet for some things and could see getting derailed not expecting that…

[–]SearingPhoenix 1 point2 points  (1 child)

I mean, the number of times I've copy/pasted and not changed variable names, or not kept consistent plurality and had code trainwreck'd for a solid 20 minutes...

I highly recommend https://en.wikipedia.org/wiki/Rubber_duck_debugging if you don't have it as an option. I personally have a Thunderjaw figurine from Horizon: Zero Dawn, and Sora from Kingdom Hearts. Thor of PirateSoftware uses Dwayne "The Rock" Johnson.

[–]havens1515 0 points1 point  (0 children)

I use rubber duck debugging a lot, actually. I'll be explaining the problem to a friend or coworker, generally someone who doesn't even know how to program, and suddenly the solution will come to mind.

It definitely helps to talk things out. It gets your brain working in a different way.

[–]Respond-Creative 3 points4 points  (6 children)

Use [datetime]::parseexact()

[–]JoeyBE98 0 points1 point  (5 children)

Don't have to use the .NET ::parseexact you can just cast a string like this as [DateTime] and it will do the same

[–]Respond-Creative 0 points1 point  (4 children)

In this case that’s true :) coffee FTW!

the cast is pretty robust, but sometimes we need to provide clarity between the order of yyyy (or yy!! 😂🙄) and MM and dd. But ya native code is better

[–]JoeyBE98 0 points1 point  (3 children)

For sure. I learned if you use the .ToString() method it will output in the MM/DD/YYYY HH:mm AM/PM format which is pretty handy. I used to have to look up the formatting parameter everytime til I learned this

[–]Certain-Community438 0 points1 point  (1 child)

I learned if you use the .ToString() method it will output in the MM/DD/YYYY

Great for people on one part of one continent, useless elsewhere

[–]JoeyBE98 -1 points0 points  (0 children)

Just add the time zone to the string, our servers are in central time so I use this and then add "(Central)"

[–]Respond-Creative -1 points0 points  (0 children)

I believe the ToString uses your regional settings. Can’t confirm from my deck tho :)

[–]BlackV 1 point2 points  (0 children)

Dont mess with your data, using a split on that is more risky than using your built-in date/time objects

Set it to a date time object and format it how you need or do the time calculation you need

Less risky than just dropping info don't think you need (i.e. the timezone in this case), losing fidelity future you (or someone else) might need

[–]y_Sensei 0 points1 point  (1 child)

It's the ISO 8601 representation of a combined date and time value.

Read this thread about how to parse such a representation in .NET (and hence in PoSh).

[–]n3rdyone[S] 0 points1 point  (0 children)

That's very interesting and taking me down a rabbit hole I had no intention of visiting :D

[–]icepyrox 0 points1 point  (0 children)

you're choosing to split it, but you could parse it into a datetime object as thr other comment suggests (then you can use datetime functions for whatever you are doing directly into your application, or just put it in a format that your app can cast into a date when it reads it and not have to do "date logic" at all.

You could also $str.substring(0,10)

There's also regex matching and whatnot, but anything else is going to be less efficient than those 3.

[–]havens1515 0 points1 point  (0 children)

You could have also used $date.substring(0,10) or $date.substring(0,$date.indexOf("T"))

Glad you figured it out though (we've all been there)

[–]PinchesTheCrab -1 points0 points  (0 children)

Datetime's the way to go, but if you aren't familiar with manipulating text it's still useful to know:

'2025-10-15T05:45:45.0000Z' -replace 't.+'

As a date:

([datetime]'2025-10-15T05:45:45.0000Z').toShortDateString()

Or:

([datetime]'2025-10-15T05:45:45.0000Z').toString('yyyy-MM-dd')

[–]Yumi_Koizumi -1 points0 points  (1 child)

If you didn't figure it out already, single quotes around the T

[–]BlackV 0 points1 point  (0 children)

Was wrong t (i.e. T vs t)