Hidei
Hidei

Reputation: 197

To compare the day , date , timestamp of two lines in file and output the latest day,date,timestamp

I have below lines from two different file having day , date ,timestamp. I need to output the line with latest timestamp.

The lines of one file :

Tue 31/12/2000 17:13:29.83 - file copied

And another file content is :

Sun 17/07/1996 12:11:14.84 - drivers updated

The output must be

Tue 31/12/2000 17:13:29.83 - file copied

How can we compare timestamp?

Upvotes: 1

Views: 399

Answers (1)

Theo
Theo

Reputation: 61068

To parse out the date from these strings you can do:

# get the date and time from the string, remove the dayname as it is incorrect for that date
$dateString = ("Sun 31/12/2000 17:13:29.83 - file copied" -split '-')[0].Substring(4).Trim()
$date1 = [datetime]::ParseExact($dateString, 'dd/MM/yyyy HH:mm:ss.ff', [CultureInfo]"en-US")

# do the same for the date in the second file and call that $date2

Then simply compare the dates using

$theDateYouWant = if ($date1 -gt $date2) { $date1 } else { $date2 }

By stripping off the dayname, you can use $null or [cultureinfo]::InvariantCulture instead of [CultureInfo]"en-US"

Upvotes: 1

Related Questions