TotalMilliseconds
(1)
RichardSiddaway
(1)
TotalSeconds
(1)
TotalMinutes
(1)
TotalHours
(1)
TotalDays
(1)
TimeSpan
(1)
PowerTip
(1)

temporary file names via Get-Date

Asked By Larry__Weiss
18-Mar-10 11:00 AM
The "PowerTip of the Day" article at
http://app.en25.com/e/es.aspx?s=1403&e=2867&elq=8322eea60ce14904acb144f2ae454b60
displayed this code to generate temporary file names

PS C:> (Get-Date -format 'yyyy-MM-dd hh-mm-ss') + '.tmp'
2010-03-18 09-48-08.tmp

I recoded it to do it this way

PS C:> (Get-Date).Ticks.ToString() + '.tmp'
634045027709549764.tmp

Now suppose I later wanted to turn that "634045027709549764" back into a legible
date-time expression like "2010-03-18 09-48-08".   Is that possible?

- Larry

PS C:\> $a = (get-date).ticks.

PaulChavez replied to Larry__Weiss
18-Mar-10 11:44 AM
PS C:\> $a = (get-date).ticks.tostring()

PS C:\> $a
634044984512885070

PS C:\> get-date ([long]$a)

Thursday, March 18, 2010 8:40:51 AM


-Paul

Thanks!

Larry__Weiss replied to PaulChavez
18-Mar-10 12:43 PM
Thanks!  I did not expect it to be so straightforward.

That makes me wonder about the units involved in ticks.
How many ticks equal one second?

I guess I can get PowerShell to tell me:

PS C:> $a1 = [datetime] "March 18, 2010 11:23:01 AM"
PS C:> $a1n = [long](($a1).ticks.tostring())
PS C:> $a2 = [datetime] "March 18, 2010 11:23:02 AM"
PS C:> $a2n = [long](($a2).ticks.tostring())
PS C:> $a2n - $a1n
10000000

So there must be 10,000,000 ticks per second.

Did I do that right?

- Larry

Much easier to do thisPS> New-TimeSpan -Seconds 1Days : 0Hours

RichS [MVP] replied to Larry__Weiss
18-Mar-10 02:46 PM
Much easier to do this

PS> New-TimeSpan -Seconds 1


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 1
Milliseconds      : 0
Ticks             : 10000000
TotalDays         : 1.15740740740741E-05
TotalHours        : 0.000277777777777778
TotalMinutes      : 0.0166666666666667
TotalSeconds      : 1
TotalMilliseconds : 1000

--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog and PowerShell User Group:
http://msmvps.com/blogs/RichardSiddaway/Default.aspx
There's a cmdlet for that!
Larry__Weiss replied to RichS [MVP]
18-Mar-10 02:54 PM
- Larry
Post Question To EggHeadCafe