header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

[Datetime] shell vs script
Last Post 25 Feb 2008 04:55 PM by kscriss. 7 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
KlaasUser is Offline
New Member
New Member
Posts:11
Avatar

--
18 Feb 2008 06:25 PM  

Hi,

I'm trying to compare dates in a script. Since the dates come out of a .csv-file, I cast them to [datetime]: 

 $dag=[DateTime] $user.lastlogin

 but I get an error :

"Cannot convert value "10/09/2008 15:23" to type "System.DateTime".Error: "De tekenreeks is niet als geldige DateTime herkend.", which is Dutch for "The string is not recognised as a valid DateTime." 

However, when I type the same line at the prompt, it works perfect: when I look into $dag, it says:

"maandag 10 september 2007 15:23:00"

What's the difference between the prompt and the line in a script???

 

kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
18 Feb 2008 06:44 PM  
Here is anotherway.

$dag = get-date "10/09/2008 15:23"
$dag
$dag | get-member

The above works.

Perhaps your error message is realy saying the contents of your $user.lastlogin variable has been converted to system.string and is no longer compatible with system.datetime operations.

What does the value of $user.lastlogon look like exactly?
My blog: http://blogs.powershellcentral.com/kscriss/
kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
18 Feb 2008 07:16 PM  
Either way works for me. I tried your way below.

$TimeRightNow = get-date
$TimeBefor = "10/09/2008 15:23"
$TimeBefor | Out-Host
# $TimeBefor = Get-Date $TimeBefore
$TimeBefor =[DateTime] $TimeBefor
$TimeBefor | Out-Host
$ElapseTime = [TimeSpan]($TimeRightNow - $TimeBefor )
$ElapseTime | Out-Host
$ElapseTime = $Elapsetime.tostring()
$ElapseTime | Out-Host
If ($TimeBefor -le $TimeRightNow)
{ "The Time Before is the older of the two times" | Out-Host}
Else
{ "The Time Before is the newer of the two times" | Out-Host}
My blog: http://blogs.powershellcentral.com/kscriss/
KlaasUser is Offline
New Member
New Member
Posts:11
Avatar

--
19 Feb 2008 05:18 PM  

Get-Date $user.lastlogin works fine, thanks!

[datetime] "10/09/2008 15:24" also works, but [datetime] $user.lastlogin does not. The value of $user.lastlogin looks exactly the same: "10/09/2008 15:24".

 

bsonposhUser is Offline
Basic Member
Basic Member
Posts:393
Avatar

--
19 Feb 2008 05:26 PM  
You can try
[datetime]::parse($user.lastlogin)

Is it possible that $user.lastlogin is not a string, but perhaps a int64?
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
KlaasUser is Offline
New Member
New Member
Posts:11
Avatar

--
25 Feb 2008 08:16 AM  

I'm sorry, I think I 've chosen a wrong example above. It seems to be a culture thing:

get-date "15/09/2007" works fine, get-date "09/15/2007" does not, while

[datetime] "15/09/2007" does not work, [datetime] "09/15/2007" does, but

[datetime]::parse("15/09/2007") works, and [datetime]::parse("09/15/2007") does not.

kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
25 Feb 2008 04:51 PM  
This might be helpfull.


# function ParseDate
# Author: Michael Bednarz
# I found this function on the web.  I believe his site is a Polish language web site, which I
# can't read, but I found no difficulity reading his PowerShell.  His scripts are cleanly written.
# Reference: http://wss.pl/Articles/7914.aspx   
{
   param( $dateWMI )
   $year = [int]$dateWMI.substring(0,4)
   $month = [int]$dateWMI.substring(4,2)
   $day = [int]$dateWMI.substring(6,2)
   $hour = [int]$dateWMI.substring(8,2)
   $minute = [int]$dateWMI.substring(10,2)
   $second = [int]$dateWMI.substring(12,2)
   "$year $month $day $hour $minute $second" | out-host
   return new-object DateTime $year, $month, $day, $hour, $minute, $second
 }
My blog: http://blogs.powershellcentral.com/kscriss/
kscrissUser is Offline
Basic Member
Basic Member
Posts:119

--
25 Feb 2008 04:55 PM  
I shouldn't have the #function ParseDate commented out. I think the top line should read.

function ParseDate
My blog: http://blogs.powershellcentral.com/kscriss/
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer