Title: Add Format alias for the ToString method to all scalar objects · Issue #14372 · PowerShell/PowerShell · GitHub
Open Graph Title: Add Format alias for the ToString method to all scalar objects · Issue #14372 · PowerShell/PowerShell
X Title: Add Format alias for the ToString method to all scalar objects · Issue #14372 · PowerShell/PowerShell
Description: This idea arose from this from the stackoverflow question Convert datetime formats using powershell and is related to #7445 We need a new operator '..' to access to members of childitems of a collection but specific to the ToString metho...
Open Graph Description: This idea arose from this from the stackoverflow question Convert datetime formats using powershell and is related to #7445 We need a new operator '..' to access to members of childitems of a colle...
X Description: This idea arose from this from the stackoverflow question Convert datetime formats using powershell and is related to #7445 We need a new operator '..' to access to members of childitems of...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/14372
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add Format alias for the ToString method to all scalar objects","articleBody":"This idea arose from this from the stackoverflow question [Convert datetime formats using powershell](https://stackoverflow.com/q/65212907/1701026) and is related to #7445 [We need a new operator '..' to access to members of childitems of a collection](https://github.com/PowerShell/PowerShell/issues/7445) but specific to the `ToString` method.\r\n\r\nVariable definitions (used in this request)\r\n```PowerShell\r\n$Strings = 'Monday, November 30, 2020 7:55:01 AM', 'Sunday, October 18, 2020 9:10:01 PM', 'Sunday, November 8, 2020 9:40:34 PM'\r\n$DateTimes = [DateTime[]]$Strings\r\n```\r\n\r\nKnowing that `ToString` isn't just parameterless but also has an **_usefull_** `IFormatProvider` interface parameter, I wanted to prove that you can use member enumerating for the stackoverflow question (instead of iterating through each item), but I failed to do so, as I keep ending up with a (new) collection that has it's own (parameterless) `ToString` method.\r\n\r\nAs @mklement0 commented in #7445:\r\n\r\n> `ToString` is currently invariably invoked on the collection itself (_where it is more or less useless_)\r\n\r\n- If it is indeed useless, why not break it?\r\nThat probably goes too deep as it is not just PowerShell but the `ToString` method actually appears to be implemented in all the .Net collections.\r\n\r\n- If it can't be broken, why not bend it? Meaning, only apply the method to the collection if it has no arguments, which would let the developer choose between the collection (`$DateTimes.ToString()`) or a the embedded scalars (`$DateTimes.ToString('')`) , knowing that currently this curectly returns an error.\r\nBut that is probably too inconsistent and confusing...\r\nHmm, talking about inconsistency...\r\n\r\n```PowerShell\r\n$DateTimes[0].ToLocalTime()\r\nMonday, November 30, 2020 8:55:01 AM\r\n\r\n$DateTimes[0..2].ToLocalTime()\r\nMonday, November 30, 2020 8:55:01 AM\r\nSunday, October 18, 2020 11:10:01 PM\r\nSunday, November 8, 2020 10:40:34 PM\r\n\r\n$DateTimes[0].ToString('yyyy-MM-dd HH:mm:ss')\r\n2020-10-18 21:10:01\r\n\r\n$DateTimes[0..2].ToString('yyyy-MM-dd HH:mm:ss')\r\nMethodException: Cannot find an overload for \"ToString\" and the argument count: \"1\".\r\n```\r\n\r\n- If it can't be broken or bended, why not equipping all scalar objects (that have a `ToString` method and explicitly excluding collections) with an `Format` alias for the `ToString` method, so that it is possible to enumerate string formats like (wishful thinking):\r\n\r\n```PowerShell\r\n$DateTimes.Format('yyyy-MM-dd HH:mm:ss')\r\n2020-11-30 07:55:01\r\n2020-10-18 21:10:01\r\n2020-11-08 21:40:34\r\n```\r\n","author":{"url":"https://github.com/iRon7","@type":"Person","name":"iRon7"},"datePublished":"2020-12-10T10:29:48Z","url":"https://github.com/PowerShell/PowerShell/issues/14372"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7dc9b950-005b-b8c5-185d-410d135de8e3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A2A4:35BEEA:4A406:686D2:6AFDA8EA |
| html-safe-nonce | c7dc70ef3c98c66dbfbd48075dc19897d94bc1264690d4fecd9418a5e9796142 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMkE0OjM1QkVFQTo0QTQwNjo2ODZEMjo2QUZEQThFQSIsInZpc2l0b3JfaWQiOiIxMDkyMjg1MzI1NjIwNjUyMjY2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1ea6521ea65ffc7d1b7fbd10612a2ef0130fbbd7bbad38d0634566906fa3a3fa |
| hovercard-subject-tag | issue:761114995 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/14372/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f9f4e7c1744988f1ffa36ce0faf257c11fd7371e0878afd71a2f1619f0b76c79/PowerShell/PowerShell/issues/14372 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f9f4e7c1744988f1ffa36ce0faf257c11fd7371e0878afd71a2f1619f0b76c79/PowerShell/PowerShell/issues/14372 |
| og:image:alt | This idea arose from this from the stackoverflow question Convert datetime formats using powershell and is related to #7445 We need a new operator '..' to access to members of childitems of a colle... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | iRon7 |
| hostname | github.com |
| expected-hostname | github.com |
| None | bc86424ef34a73b386237872800e922d149d65ba907f63d6f150a2dcb8433bea |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 0ddf53eb35c87f1f8df13dbf75c3bdadbfc6e969 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width