Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Converting date time format to string format

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-18-2007   #1 (permalink)
IT Staff
Guest


 

Converting date time format to string format

See Part 1 of this code

==================================================================
# both are in date time format
$created = get-date -year 2007 -month 12 -day 1
$created1 = get-date
# converted to string format
$a = $created.tostring("f")
$aa = $created1.tostring("f")
# assign a variable to hold all values, separated by comma
$grouparray = ($a,$aa)
$group = $grouparray | foreach
{([regex]'([A-za-z]+\s[0-9]+,\s[0-9]+)').match($_).value}
$group | group -noelement | select name, count | sort -property name
==================================================================
Result i got is :

Name
Count
---- -----
December 01, 2007
1
December 18, 2007
1


==================================================================
See Part 2 of this code
==================================================================
# in date time format
$created = get-date -year 2007 -month 12 -day 1
$files = get-childitem d:\backup | ? {!$_.PSIsContainer -and
($_.lastwriteTime -ge $created)} | select lastwriteTime | foreach
{$_.lastwriteTime}

# still in date time format
$files

Results show below :
==================================================================
Tuesday, December 04, 2007 12:34:55 PM
Tuesday, December 04, 2007 12:09:04 PM
Monday, December 10, 2007 1:57:47 PM
Sunday, December 09, 2007 5:31:06 PM
Tuesday, December 11, 2007 1:42:29 PM
Tuesday, December 04, 2007 3:56:21 PM
Wednesday, December 05, 2007 10:13:43 AM
Tuesday, December 04, 2007 11:57:49 AM
Tuesday, December 04, 2007 10:07:32 AM
Friday, December 07, 2007 7:44:57 PM
Monday, December 10, 2007 12:21:33 PM
Tuesday, December 11, 2007 1:29:50 PM
Monday, December 10, 2007 2:21:37 PM

==================================================================
Here are the questions :

a) In part 2 of the code, i want to convert $files into a string format. I
try to do this :

$abc = $files.tostring("f")

This does not work. Error Cannot find an overload for "ToString" and the
argument count: "1". I think the tostring method expects a "1" argument
instead of multiple lines arguement. How do i convert $files into similar
Part 1 of the code as in below :

# assign a variable to hold all values, separated by comma
# this is a string format i want.
$grouparray = ($a,$aa)














My System SpecsSystem Spec
Old 12-18-2007   #2 (permalink)
Shay Levi
Guest


 

Re: Converting date time format to string format

Add ToString("f") on the foreach:

$created = get-date -year 2007 -month 12 -day 1
$files = get-childitem d:\backup | ? {!$_.PSIsContainer -and
($_.lastwriteTime -ge $created)} | select lastwriteTime | foreach
{$_.lastwriteTime.tostring("f")}


To make it clear

## $files contains array of datetime objects
$files = dir | select lastwritetime


## check each type of object in $files (not a string)
$files | foreach {$_.gettype()}

## now $files contains date strings
$files = dir | foreach {$_.lastwritetime.tostring("f")}


## check again each object in the array for its type, its all strings
$files | foreach {$_.gettype()}


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> See Part 1 of this code
>
> ==================================================================
> # both are in date time format
> $created = get-date -year 2007 -month 12 -day 1
> $created1 = get-date
> # converted to string format
> $a = $created.tostring("f")
> $aa = $created1.tostring("f")
> # assign a variable to hold all values, separated by comma
> $grouparray = ($a,$aa)
> $group = $grouparray | foreach
> {([regex]'([A-za-z]+\s[0-9]+,\s[0-9]+)').match($_).value}
> $group | group -noelement | select name, count | sort -property name
> ==================================================================
> Result i got is :
> Name
> Count
> ----
> -----
> December 01, 2007
> 1
> December 18, 2007
> 1
> ==================================================================
> See Part 2 of this code
> ==================================================================
> # in date time format
> $created = get-date -year 2007 -month 12 -day 1
> $files = get-childitem d:\backup | ? {!$_.PSIsContainer -and
> ($_.lastwriteTime -ge $created)} | select lastwriteTime | foreach
> {$_.lastwriteTime}
> # still in date time format
> $files
> Results show below :
> ==================================================================
> Tuesday, December 04, 2007 12:34:55 PM
> Tuesday, December 04, 2007 12:09:04 PM
> Monday, December 10, 2007 1:57:47 PM
> Sunday, December 09, 2007 5:31:06 PM
> Tuesday, December 11, 2007 1:42:29 PM
> Tuesday, December 04, 2007 3:56:21 PM
> Wednesday, December 05, 2007 10:13:43 AM
> Tuesday, December 04, 2007 11:57:49 AM
> Tuesday, December 04, 2007 10:07:32 AM
> Friday, December 07, 2007 7:44:57 PM
> Monday, December 10, 2007 12:21:33 PM
> Tuesday, December 11, 2007 1:29:50 PM
> Monday, December 10, 2007 2:21:37 PM
> ==================================================================
> Here are the questions :
>
> a) In part 2 of the code, i want to convert $files into a string
> format. I try to do this :
>
> $abc = $files.tostring("f")
>
> This does not work. Error Cannot find an overload for "ToString" and
> the argument count: "1". I think the tostring method expects a "1"
> argument instead of multiple lines arguement. How do i convert $files
> into similar Part 1 of the code as in below :
>
> # assign a variable to hold all values, separated by comma
> # this is a string format i want.
> $grouparray = ($a,$aa)

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Birthday field date and time format New Vista user Vista mail 1 04-06-2008 10:19 PM
converting audio format dd Vista music pictures video 3 03-06-2008 05:25 AM
Photo Gallery Date/Time Format jeff Vista file management 0 09-11-2007 05:18 PM
ADO and String.Format questions. Kevin Burton PowerShell 4 11-22-2006 07:11 AM
string.format() ??? Jeff Jarrell PowerShell 2 05-20-2006 09:20 AM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51