.NET Framework - 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
TotalMilliseconds
(1)
RichardSiddaway
(1)
TotalSeconds
(1)
TotalMinutes
(1)
TotalHours
(1)
TotalDays
(1)
TimeSpan
(1)
PowerTip
(1)
PaulChavez replied to Larry__Weiss
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
Larry__Weiss replied to PaulChavez
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
RichS [MVP] replied to Larry__Weiss
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
Larry__Weiss replied to RichS [MVP]
1) Process.KernelModeTime (1) Vista (1) PrivilegedProcessorTime (1) UserProcessorTime (1) KernelModeTime (1) Sample.SystemFrequency (1) TotalMilliseconds (1) Not that I'm aware of. I've just done a get-process on Please note that all scripts are supplied "as is" and with no warranty Blog: http: / / richardsiddaway.spaces.live.com / PowerShell User Group: http: / / www.get-psuguk.org.uk It does show Seconds : 20 Milliseconds : 718 Ticks : 134007187500 TotalDays : 0.155100911458333 TotalHours : 3.722421875 TotalMinutes : 223.3453125 TotalSeconds : 13400.71875 TotalMilliseconds : 13400718.75 PS> Casting: [timeSpan][long](gwmi Win32_Process -f "Name = 'System idle process'").KernelModeTime - - Kiron
after subtraction? Thank you, .NET Compact Framework Discussions TimeSpan (1) CompareTo (1) Mytimespan.CompareTo (1) TotalMilliseconds (1) TotalMinutes (1) TotalSeconds (1) TotalHours (1) TotalDays (1) You can check any of the total values: TotalDays, TotalHours, TotalMinutes, TotalSeconds, TotalMilliseconds. If the time is only a few milliseconds different then all of these values will be zero, except TotalMilliseconds, which will be negative. Rick D. Contractor Thank you, But i do not see how
DateTime structs) represent times about or less than one second apart, then using: (dt1 - dt0).TotalSeconds (or TotalMilliSeconds, or dt1.Subtract (dt0)) returns 0.0 *or* 1.0 whereas on the desktop, .NET DateTime structs) represent times about or less than one second apart, then using: (dt1 - dt0).TotalSeconds (or TotalMilliSe
used to take much more time than what a loop body should actually do. Example: TotalMilliseconds : 10.8296 TotalMilliseconds : 170.6035 Can anybody repro \ confirm this? - - Thanks, Roman Kuzmin Powershell Discussions XP (1) StartTime (1) ContinueException (1) EndTime (1) TotalMilliseconds (1) DoWhileStatementNode (1) TotalSeconds (1) FlowControlNode (1) Yes, it's about 20x slower for me too. Is continue an for($e = 0; ; ++$e) { if ($e -ge 1000) { break } } [datetime]$endTime = [datetime]::now ($endTime - $startTime).TotalSeconds } function time2 { [datetime]$startTime = [datetime]::now for($e = 0; ; ++$e) { if ($e -ge 1000) { break } else { continue } } [datetime]$endTime = [datetime]::now ($endTime - $startTime).TotalSeconds } 0.01 v 0.2 If I had to guess, I'd say it's