Jagbir Singh
Jagbir Singh

Reputation: 39

How to Convert datetime formats using powershell

I Have a variable stored with this value

PS C:\Users\> $Time

Monday, November 30, 2020 8:55:01 AM
Sunday, October 18, 2020 11:10:01 PM
Sunday, November 8, 2020 10:40:34 PM
Sunday, November 8, 2020 11:47:37 PM
Sunday, November 8, 2020 10:59:57 PM
Tuesday, December 1, 2020 3:15:42 AM
Monday, November 30, 2020 7:00:32 PM
Monday, November 30, 2020 12:19:06 AM
Monday, November 30, 2020 7:01:34 PM
Tuesday, December 1, 2020 1:12:10 AM
Tuesday, December 1, 2020 2:37:18 AM
Sunday, November 1, 2020 7:39:34 PM
Sunday, September 27, 2020 11:48:38 PM

I want the time formats of the variable $time to change to "yyyy-mm-dd hh:mm:ss" so that ALL of the list is displayed

PS C:\Users\> $Time

2020-11-30 08:55:01
2020-10-18 11:10:01
2020-11-08 10:40:34
2020-11-08 11:47:37
2020-11-08 10:59:57
2020-12-01 03:15:42
2020-11-30 07:00:32
2020-11-30 12:19:06
2020-11-30 07:01:34
2020-12-01 01:12:10
2020-12-01 02:37:18
2020-11-01 07:39:34
2020-09-27 11:48:38

Please help me creating a code for the same

thanks

Upvotes: 0

Views: 5977

Answers (1)

Chad Baldwin
Chad Baldwin

Reputation: 2602

This is a pretty common question that gets asked here on StackOverflow, however, it seems most of the answers are directed towards converting a variable which stores a single date, to a formatted string.

Whereas you have an array of dates you want to convert.

I'm going to make the assumption that you have an Array of DateTime values, and not an Array of String.

For starters, there's TONS of blogs and articles about this, not to mention the documentation.

Depending on how you need to use this data there is a million different ways to do this.

Primarily, you need to learn how to perform actions against an array of objects. Using things like ForEach, ForEach-Object, Select-Object, etc. Once you learn how to use those, then the problem just becomes "how do you format a date to a string", which is all over the place on here and the rest of the internet.

Here's some examples:

# Use this to generate sample data:
$Time = 10000,9000,8000,7000,6000,5000,4000,3000,2000,1000 |
    ForEach-Object { (Get-Date).AddMinutes(-$_) }

## Various solutions:
$Time | ForEach-Object { $_.ToString('yyyy-MM-dd HH:mm:ss') }

$Time | ForEach-Object { $_ | Get-Date -Format 'yyyy-MM-dd HH:mm:ss' }
$Time | ForEach-Object { $_ | Get-Date -f 'yyyy-MM-dd HH:mm:ss' }
$Time | ForEach-Object { Get-Date $_ -f 'yyyy-MM-dd HH:mm:ss' }

$Time | Select-Object @{N='TimeString'; E={$_.ToString('yyyy-MM-dd HH:mm:ss')}}

foreach ($tv in $time) { $tv.ToSTring('yyyy-MM-dd HH:mm:ss') }

$Time.ForEach({$_.ToString('yyyy-MM-dd HH:mm:ss')})

# Other methods submitted in comments, thanks @iRon
$Time | ForEach-Object ToString('yyyy-MM-dd HH:mm:ss')
$Time.ForEach('ToString', 'yyyy-MM-dd HH:mm:ss')

Note that this is case sensitive.

  • MM - Means the two digit month value
  • mm - Means the two digit day value

Upvotes: 5

Related Questions