Unable to compare dates of different formats in PowerShell

powershell compare dates without time
powershell compare date difference in days
powershell datetime format
powershell comparing 2 dates
powershell where-object date greater than
powershell convert string to date
compare date times powershell
powershell date math

I have to update objects in a data set by the last modified date. The (potentially) updated objects come from a REST api call and the objects to be updated from a SharePoint list.

Unfortunately, I'm getting way different date/time formats for each identical item in PowerShell. Compare:

Rest call results: 2016-12-15T08:08:39.012+01:00 SharePoint list: Thursday, December 15, 2016 7:08:39 AM

I was thinking of using the [datetime]::ParseExact method but a) I don't know what the input format has to be and b) if that even helps me to compare the two afterwards.

I'd appreciate your help, thanks.

I'd like to address a few issues:

  1. You can always use [System.DateTime]::Parse() for each of the formats. You don't need to use [System.DateTime]::ParseExact().

  2. You can use Get-Date instead of C#. Use native features whenever possible. Get-Date $SomeDate will always give you the same results with [System.DateTime]::Parse($SomeDate).

  3. The unnamed issue is one of the formats care about TimeZones but other one does not. You need to work on that.

Comparing dates, Comparing dates DateTime objects may be compared using PowerShell's casting with parameters, a great deal of care is required for date formats other than US. For example, in the UK I might write the following, yet the comparison will fail. This converts the string back to a date format. This should allow you to use a string when needed and a full date format when doing a comparison. I am using PowerShell in Orchestrator to compare termination dates stored as a string in an AD attribute to the current date. The purpose is to automate deletion of an AD account.

you can store both the date in variable and change it to the specific format for example Get-Date $date -Format "yyyy-MM-dd hh:mm:ss" and then you can compare the dates for example

$date="Thursday, December 15, 2016 7:08:39 AM"
Get-Date $date -Format "yyyy-MM-dd hh:mm:ss"

$date="2016-12-15T08:08:39.012+01:00"
Get-Date $date -Format "yyyy-MM-dd hh:mm:ss"

for both output would be in same format

Topic: Date Comparison – Strangely failing, Date Comparison – Strangely failing. Welcome ToString() comparing, other without it is not working. Is there any I fixed it by using -format d. Unfortunately, I'm getting way different date/time formats for each identical item in PowerShell. Compare: Rest call results: 2016-12-15T08:08:39.012+01:00 SharePoint list: Thursday, December 15, 2016 7:08:39 AM. I was thinking of using the [datetime]::ParseExact method but a) I don't know what the input format has to be and b) if that even

You can use Get-Date to convert the strings to [DateTime] objects, which can then be easily compared:

$date1 = Get-Date '2016-12-15T08:08:39.012+01:00'
$date2 = Get-Date 'Thursday, December 15, 2016 7:08:39 AM'

$date1 -gt $date2
True

$date1 -lt $date2
False

[SOLVED] Compare dates and do something, toString("yyyy-M-dd") $today = Get-Date -format yyyy-M-dd if($today -ge $​startdate -and $today -le $enddate){ Ideally, don't convert dates to strings if you are going to compare them. This third condition is separate from the other condition. As I said earlier, it is possible to use PowerShell to make date comparisons. Although you can use the -Format parameter to put the date into a specific format prior to comparing it to another value, doing so is not always necessary. For example, comparing file write dates does not require the use of any special formatting.

Thanks for the explanations, that was very helpful. I managed to compare and adjust for the timezone differences the following way:

$date1 = Get-Date '2016-12-15T08:08:39.012+01:00' $date2 = (Get-Date 'Thursday, December 15, 2016 7:08:39 AM').AddHours(1)

By comparing the .DateTime property I will get the evaluation to be true:

$date1.DateTime -eq $date2.DateTime True

Comparison of DateTime objects not working expectedly. : PowerShell, When storing (Get-Date -Format 'MM/dd/yyyy') in a variable and using correctly, the operators aren't performing as expected, or I'm making some other error. Full date and time (long date and short time) F . FullDateTimePattern (long date and long time) g . General (short date and short time) G . General (short date and long time) m, M . MonthDayPattern . o, O . Round-trip date/time pattern; with this format pattern, the formatting or parsing operation always uses the invariant culture. r, R

how to compare date in IF condition, I have the other variable in dd/mm/yyyy format. can someone confirm comparison, Powershell has to be able to parse a date/time string into a  This article contains powershell scripts to find difference between two dates. You can show the difference in Days, Hours, Minutes and Seconds.

Working with dates in PowerShell: Tips and tricks, Here's some tips on how to work with dates in PowerShell. If you need to display the date in a certain way, then PowerShell offers a number of different options for doing so. to put the date into a certain format for the purpose of making a comparison. But wouldn't it be nice to be able to verify that? I can also display a standard date output with respect to the local culture settings, but it does not appear to work properly. So I use the .NET formatting specifiers for this. This is shown here: PS C:> Use-Culture de-DE {get-date -UFormat %x} 01.22.15. PS C:> Use-Culture de-DE {get-date -Format d} 22.01.2015

Powershell - Comparing Date Variables, Powershell - Comparing Date Variables using these types of question: was I actually getting a correct file count and date time: I expect it to fail before 11am, but, it never returned $true . They were the same format. Get-Date can format the date and time in several.NET and UNIX formats. You can use Get-Date to generate a date or time character string, and then send the string to other cmdlets or programs. Get-Date uses the computer's culture settings to determine how the output is formatted. To view your computer's settings, use (Get-Culture).DateTimeFormat.