 |
|
| IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012PowerShellCommunity.org is moving! This community software, and the hardware that it sits on, are no longer serving the purposes of this community. As a result, we have decided to move this community to a new home at PowerShell.org. PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site. We've already started getting some great questions from members of the community over there so please, come on over and join us!
While we are going through this transition, this site will remain up for the short term. New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.
[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.
If you have any questions, please let us know on the PowerShell.org site.
Thank you,
Kirk "Poshoholic" Munro |
|
|
|
|
Comparing two different dates
Last Post 20 Mar 2012 11:01 AM by Zushakon. 10 Replies.
|
Sort:
|
|
Prev Next |
You are not authorized to post a reply. |
|
Zushakon
 New Member Posts:15

 |
| 11 Mar 2012 05:27 AM |
|
Hi,
I'm having a problem with comparing two different dates. One is string format taken from some .xml file for example:
Sun Feb 19 03:31:49 2012
the second one is from get-date:
11 marca 2012 14:19:27
I'm need to take from .xml file last 24 hours logs but to be honest i dont know how to compare it. Any suggestions?
|
|
|
|
|
djh53
 New Member Posts:31

 |
| 12 Mar 2012 02:40 AM |
|
You can call VBA from a Powershell routine. That would allow you to use the DateSerial function, which takes three integers as arguments: year, month and day.
The xml piece is a little harder, but not much. You need to determine if the XML's format is always the same: 3-char day, followed by space, followed by 2-char date, terminate with 4-char year. If this is so, writing a parser to get year, month and day is easy. If not, it will probably still be doable, just more tedious.
A Dateserial is a unique number assigned to a date. Having had to compare dates in the past, I came to regard it as the surest way to establish a date order.
In what language does "marca" mean "March"? |
|
|
|
|
EBGreen
 Veteran Member Posts:1276

 |
| 12 Mar 2012 06:31 AM |
|
Assuming that marca is a localization of march, to keep it all in Powershell: $date1 = Get-Date ('Sun Feb 19 03:31:49 2012' -replace '(.*) (.*) (.*) (.*) (.*)', '$3 $2 $5 $4') $date2 = Get-Date '11 march 2012 14:19:27' $date1 - $date2 Days : -21 Hours : -10 Minutes : -47 Seconds : -38 Milliseconds : 0 Ticks : -18532580000000 TotalDays : -21.4497453703704 TotalHours : -514.793888888889 TotalMinutes : -30887.6333333333 TotalSeconds : -1853258 TotalMilliseconds : -1853258000 |
|
| "Look Ma...no strings!" |
|
|
cameronove
 Basic Member Posts:368

 |
| 12 Mar 2012 09:32 AM |
|
@EBGreen Dude you need to write a book on regex. This isn't the first post where you displayed black-belt ninja regex skills. #Outstanding!!!
|
|
|
|
|
EBGreen
 Veteran Member Posts:1276

 |
| 12 Mar 2012 09:41 AM |
|
There are much better regex folks around here. It is very easy to overuse them as well, but in this case it seems like a good solution.
There are two things that you need to know about regexes. First, this:
Is the only book that you ever need and more than 90% of the people in the world need. Second, this:
Is the best blog post ever about regular expressions. In summation, they are awesome but can kill you if used wrong. For what it is worth, the first language that I used professionally beyond batch files was Perl :) |
|
| "Look Ma...no strings!" |
|
|
cameronove
 Basic Member Posts:368

 |
| 12 Mar 2012 09:44 AM |
|
@EBGreen I take that last comment back. You need to write a book on PowerShell RegEx Not only do you display an expert knowledge on regex, but also how PowerShell takes advantage of it. I'm a huge regex fan, but that post just blew me away. As a matter of fact it opened a door wide for me that was partially opened from another post you made that demonstrated regex grouping. My personal belief is that one of the most powerful features of PowerShell is its implementation of regex and it seems that I'm just barely scratching the surface with what I'm doing with it.
|
|
|
|
|
djh53
 New Member Posts:31

 |
| 12 Mar 2012 08:48 PM |
|
That is neat.
I haven't read Friedl, but did download Appleman's eBook on regular expressions over a decade ago. A change in career path led out of programming, and I never gave the eBook more than a cursory look. You've re-kindled my interest!
Tried Spanish, Portuguese, French, Italian and Romanian, but can't find "marca". Maybe he'll tell us. |
|
|
|
|
Zushakon
 New Member Posts:15

 |
| 14 Mar 2012 11:48 AM |
|
EBGreen You are a genius ! Big thx, it works now like a charm ! Sorry for answering so late by the way. Is is possible to explain how exatly does the whole line works : Get-Date ('Sun Feb 19 03:31:49 2012' -replace '(.*) (.*) (.*) (.*) (.*)', '$3 $2 $5 $4') ? I understand that is changes localization from English to Polish but what exatly does '$3 $2 $5 $4' ? And to answer the question about "marca" it is change form from word "marzec" and it is in Polish :) |
|
|
|
|
djh53
 New Member Posts:31

 |
| 14 Mar 2012 09:41 PM |
|
Chuckle! Polish - I knew that (not).
It's best for EB to answer your question. However, if you're interested in trying to sort it out yourself, try this:
$date1 = Get-Date ('Sun Feb 19 2012' -replace '(.*) (.*) (.*) (.*)', '$3 $2 $4') $date2 = Get-Date '11 march 2012' $date1 - $date2
Compare the output to the output from EB's example. Dawn may break! |
|
|
|
|
EBGreen
 Veteran Member Posts:1276

 |
| 15 Mar 2012 01:06 PM |
|
So to answer the question, this bit '(.*) (.*) (.*) (.*) (.*)' is a regex pattern that basically breaks the strings on the spaces. To be honest, you could easily use Split() to do this: $dateParts = 'Sun Feb 19 03:31:49 2012'.Split(' ') $date1 = Get-Date "$($dateParts[2]) $($dateParts[1]) $($dateParts[4]) $($dateParts[3])" I just happened to be in a regex mood. In this case the key bit is the (.*) and the $1, $2, $3, etc. bits. The (.*) causes the regex engine to group a set of atoms that match into a group. By default the groups are simply assigned to numbered variables starting with the first group being $1 and counting up to however groups there are in the pattern. You can even provide a name for the groups if you want. As a matter of fact if I were writing a script that would potentially be maintained by someone other than myself, explicit group names would make there job much easier: $date1 = Get-Date ('Sun Feb 19 03:31:49 2012' -replace '^(?.*) (?.*) (?.*) (? |
|
| "Look Ma...no strings!" |
|
|
Zushakon
 New Member Posts:15

 |
| 20 Mar 2012 11:01 AM |
|
Now i see, i must say its quite brilliant :) I have to test it like djh53 wrote to get the full picture behind it and learn it but know i understand whats behind this combination. Big thx for help ! |
|
|
|
|
| You are not authorized to post a reply. |
|
Active Forums 4.3
|
|
 |