<#
.SYNOPSIS
This script demonstrates formatting System.TimeSpan objects
Using PowerShell
.DESCRIPTION
This scipt re-writes some MSDN Samples that demostarate
timespan formatting - the original article lacks PowerShell
Examples. And sadly, the MSDN page no longer accepts community
additions.
.NOTES
File Name : Show-TimeSpanFormatting
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 3.0
.LINK
This script posted to:
http://pshscripts.blogspot.co.uk/2014/01/show-timespanformattingps1.html
Reference MSDN Page
http://msdn.microsoft.com/en-us/library/ee372286%28v=vs.110%29.aspx
.EXAMPLE
Psh> .\Show-TimeSpanFormatting.ps1
Example 1
Time of Travel: 1.12:24:02
Time of Travel: 1.12:24:02
Example 2
Converted '1.03:14:56.1667' to 1.03:14:56.1667000
Converted '1.03:14:56.1667' to 1.03:14:56.1667000
Example 3
07:45:16 - 18:12:38 = -10:27:22
07:45:16 + 18:12:38 = 1.01:57:54
00:01:14.3650000 + 00:00:00.2143756 = 00:01:14.5793756
Example 4
7:45:16 - 18:12:38 = -10:27:22
7:45:16 + 18:12:38 = 1:1:57:54
0:01:14.036 + 0:00:00.2143756 = 0:01:14.2503756
Example 5
0:07:45:16.0000000 - 0:18:12:38.0000000 = -0:10:27:22.0000000
0:07:45:16,0000000 + 0:18:12:38,0000000 = 1:01:57:54,0000000
0:00:01:14.0360000 + 0:00:00:00.2143756 = 0:00:01:14.2503756
#>
# Show Time Span Format Strings
# Example 1 - use both tostring() and -f operators
"Example 1"
# Create Timespan object
$duration = New-Object System.TimeSpan 1, 12, 23, 62
# Now output using both tostring() and -f
"Time of Travel: " + $duration.ToString("c")
"Time of Travel: {0:c}" -f $duration
# Example 2
# Demonstrate the use of ParseExact and TryParseExact
"Example 2"
$value = "1.03:14:56.1667"
$interval = New-Object System.TimeSpan
Try {
$interval = [System.TimeSpan]::ParseExact($value, "c", $null)
"Converted '{0}' to {1}" -f $value, $interval
}
Catch [System.FormatException] {"{0}: Bad Format" -f $value}
Catch [System.OverflowException] {"{0}: Out of Range" -f $value }
If ([System.TimeSpan]::TryParseExact($value, "c", $null, [ref] $interval)) {
"Converted '{0}' to {1}" -f $value, $interval
}
Else {
"Unable to convert {0} to a time interval." -f $value
}
# Example 3
# Create two TimeSpan objects, perform arithmetic operations
# on them then displays the result using the 'C' format specifier
"Example 3"
$interval1 = New-Object System.TimeSpan 7, 45, 16
$interval2 = New-Object System.TimeSpan 18, 12, 38
"{0:c} - {1:c} = {2:c}" -f $interval1,$interval2, $($interval1 - $interval2)
"{0:c} + {1:c} = {2:c}" -f $interval1,$interval2, $($interval1 + $interval2)
$interval1 = New-Object System.TimeSpan 0, 0, 1, 14, 365
$interval2 = [System.TimeSpan]::FromTicks(2143756)
"{0:c} + {1:c} = {2:c}" -f $interval1, $interval2, $($interval1 + $interval2)
# Example 4 - The "g" Format Specifier
# This specifier returns the string representation of a TimeSpan value in a compact form
# by including only the elements that are necessary.
"Example 4"
$interval1 = New-Object System.TimeSpan 7, 45, 16
$interval2 = New-Object System.TimeSpan 18, 12, 38
"{0:g} - {1:g} = {2:g}" -f $interval1, $interval2, $($interval1 - $interval2)
# do it in French
$CI = New-object System.Globalization.CultureInfo 'fr-FR'
[System.String]::Format($CI, $("{0:g} + {1:g} = {2:g}") ,
$interval1,$interval2,$($interval1 + $interval2) )
# Another interval
$interval1 = new-object System.TimeSpan 0, 0, 1, 14, 36
$interval2 = [System.TimeSpan]::FromTicks(2143756)
"{0:g} + {1:g} = {2:g}" -f $interval1, $interval2, $($interval1 + $interval2)
# Example 5
# Using the "G' Specifier
#
"Example 5"
$interval1 = new-object System.TimeSpan 7, 45, 16
$interval2 = new-object System.TimeSpan 18, 12, 38
"{0:G} - {1:G} = {2:G}" -f $interval1, $interval2, $($interval1 -$interval2)
[System.String]::Format($(New-Object CultureInfo("fr-FR")),
"{0:G} + {1:G} = {2:G}", $interval1,
$interval2, $($interval1 + $interval2))
$interval1 = new-object System.TimeSpan 0, 0, 1, 14, 36
$interval2 = [System.TimeSpan]::FromTicks(2143756)
"{0:G} + {1:G} = {2:G}" -f $interval1,$interval2, $($interval1 + $interval2)
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Showing posts with label System.TImespan. Show all posts
Showing posts with label System.TImespan. Show all posts
Sunday, 5 January 2014
Show-TimeSpanFormatting.ps1
Tuesday, 8 November 2011
Show-TimeSpan.ps1
- <#
- .SYNOPSIS
- This script, a re-implementation of an MSDN sample, shows
- details of a timespan object
- .DESCRIPTION
- This script re-implements a simple MSDN script that creates a
- timespan object and displays its properties.
- .NOTES
- File Name : Show-TimeSpan.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.timespan.aspx
- .EXAMPLE
- Psh> .\Show-TimeSpan.ps1
- 8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15
- Value of Days Component: 229
- Total Number of Days: 229.229340277778
- Value of Hours Component: 5
- Total Number of Hours: 5501.50416666667
- Value of Minutes Component: 30
- Total Number of Minutes: 330090.25
- Value of Seconds Component: 15
- Total Number of Seconds: 19,805,415
- Value of Milliseconds Component: 0
- Total Number of Milliseconds: 19,805,415,000
- Ticks: 198,054,150,000,000
- #>
- # Define two dates
- $date1 = new-object system.datetime 2010, 1, 1, 8, 0, 15
- $date2 = new-object system.datetime 2010, 8, 18, 13, 30, 30
- # Create a time Interval
- $interval = New-Timespan -start $date1 -end $date2
- #Display the interval
- "{0} - {1} = {2}" -f $date2, $date1, $interval.ToString()
- # Display individual properties of the resulting TimeSpan object.
- " {0,-35} {1,20}" -f "Value of Days Component:", $interval.Days
- " {0,-35} {1,20}" -f "Total Number of Days:", $interval.TotalDays
- " {0,-35} {1,20}" -f "Value of Hours Component:", $interval.Hours
- " {0,-35} {1,20}" -f "Total Number of Hours:", $interval.TotalHours
- " {0,-35} {1,20}" -f "Value of Minutes Component:", $interval.Minutes
- " {0,-35} {1,20}" -f "Total Number of Minutes:", $interval.TotalMinutes
- " {0,-35} {1,20:N0}" -f "Value of Seconds Component:", $interval.Seconds
- " {0,-35} {1,20:N0}" -f "Total Number of Seconds:", $interval.TotalSeconds
- " {0,-35} {1,20:N0}" -f "Value of Milliseconds Component:", $interval.Milliseconds
- " {0,-35} {1,20:N0}" -f "Total Number of Milliseconds:", $interval.TotalMilliseconds
- " {0,-35} {1,20:N0}" -f "Ticks:", $interval.Ticks
Labels:
Power,
PowerShell scripts,
system.datetime,
System.TImespan
Monday, 15 June 2009
Get-TimeTick.ps1
- <#
- .SYNOPSIS
- This script gets and displays the static Tick values from TimeSpan Object
- .DESCRIPTION
- This script
- .NOTES
- File Name : Get-TimeTick.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.timespan.tickspersecond(VS.85).aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-TimeTick.ps1
- Ticks Per:
- Day : 864,000,000,000
- Hour : 36,000,000,000
- Minute : 600,000,000
- Second : 10,000,000
- Millisecond : 10,000
- #>
- ##
- # Start of script
- ##
- # Create the values
- $tsticksperday = [system.timespan]::ticksperday
- $tsticksperhour = [system.timespan]::ticksperhour
- $tstickspermillisecond = [system.timespan]::TicksPerMillisecond
- $tsticksperminute = [system.timespan]::ticksperminute
- $tstickspersecond = [system.timespan]::tickspersecond
- # Display results nicely formatted
- "Ticks Per:"
- "Day : {0, 15}" -f $tsticksperday.tostring("###,###")
- "Hour : {0, 15}" -f $tsticksperhour.tostring("###,###")
- "Minute : {0, 15}" -f $tsticksperminute.tostring("###,###")
- "Second : {0, 15}" -f $tstickspersecond.tostring("###,###")
- "Millisecond : {0, 15}" -f $tstickspermillisecond.tostring("###,###")
- #End of Script
Labels:
powershell,
PowerShell scripts,
PowerShell V2,
System.TImespan
Thursday, 20 November 2008
Display-TimeSpan.ps1
<#
.SYNOPSIS
Creates timespan objects and displays properties for each
.DESCRIPTION
This script creates time span objects then displays
the properties of each one.
.NOTES
File Name : Display-TimeSpan.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2
.LINK
http://pshscripts.blogspot.co.uk/2008/11/display-timespanps1.html
.EXAMPLE
PSH [C:\foo]: .\Display-Timespan.ps1
This example of the TimeSpan class properties
generates the following output. It creates
several TimeSpan objects and displays the values
of the TimeSpan properties for each
TimeSpan( 1 )
Interval = 00:00:00.0000001
Days 0 TotalDays 1.15740740740741E-12
Hours 0 TotalHours 2.77777777777778E-11
Minutes 0 TotalMinutes 1.66666666666667E-09
Seconds 0 TotalSeconds 1E-07
Milliseconds 0 TotalMilliseconds 0.0001
Ticks 1
TimeSpan( 111222333444555 )
Interval = 128.17:30:33.3444555
Days 128 TotalDays 128.729552597865
Hours 17 TotalHours 3089.50926234875
Minutes 30 TotalMinutes 185370.555740925
Seconds 33 TotalSeconds 11122233.3444555
Milliseconds 344 TotalMilliseconds 11122233344.4555
Ticks 111222333444555
TimeSpan( 10, 20, 30, 40, 50 )
Interval = 10.20:30:40.0500000
Days 10 TotalDays 10.8546302083333
Hours 20 TotalHours 260.511125
Minutes 30 TotalMinutes 15630.6675
Seconds 40 TotalSeconds 937840.05
Milliseconds 50 TotalMilliseconds 937840050
Ticks 9378400500000
TimeSpan( 1111, 2222, 3333, 4444, 5555 )
Interval = 1205.22:47:09.5550000
Days 1205 TotalDays 1205.94941614583
Hours 22 TotalHours 28942.7859875
Minutes 47 TotalMinutes 1736567.15925
Seconds 9 TotalSeconds 104194029.555
Milliseconds 555 TotalMilliseconds 104194029555
Ticks 1041940295550000
FromDays( 20.84745602 )
Interval = 20.20:20:20.1980000
Days 20 TotalDays 20.8474559953704
Hours 20 TotalHours 500.338943888889
Minutes 20 TotalMinutes 30020.3366333333
Seconds 20 TotalSeconds 1801220.198
Milliseconds 198 TotalMilliseconds 1801220198
Ticks 18012201980000
#>
##
# Start of Script
##
# Constant
$HeaderFmt = "`n{0, -45}"
$DataFmt = "{0,-12}{1,8} {2,-18}{3,21}"
# Helper function
function ShowTimeSpanProperties {
param ([System.TimeSpan] $Interval = 1)
# Display the properties of the TimeSpan parameter.
"Interval = {0,21}" -f $interval
"$DataFmt" -f "Days", $interval.Days, "TotalDays", $interval.TotalDays
"$DataFmt" -f "Hours", $interval.Hours, "TotalHours", $interval.TotalHours
"$dataFmt" -f "Minutes", $interval.Minutes, "TotalMinutes", $interval.TotalMinutes
"$DataFmt" -f "Seconds", $interval.Seconds, "TotalSeconds", $interval.TotalSeconds
"$DataFmt" -f "Milliseconds", $interval.Milliseconds, "TotalMilliseconds", $interval.TotalMilliseconds
"$DataFmt" -f $null, $null, "Ticks", $interval.Ticks
}
# Start of main script
# Create and display a comment
$comment = @"
This example of the TimeSpan class properties
generates the following output. It creates
several TimeSpan objects and displays the values
of the TimeSpan properties for each
"@
$comment
# Create and display a TimeSpan value of 1 tick.
$ts = [system.TimeSpan] 1
"$HeaderFmt" -f "TimeSpan( 1 )"
ShowTimeSpanProperties($ts)
# Create a TimeSpan value with a large number of ticks.
$ts = [System.TimeSpan] 111222333444555
"$HeaderFmt" -f "TimeSpan( 111222333444555 )"
ShowTimeSpanProperties($ts)
# This TimeSpan has all fields specified.
$ts = New-Object System.TimeSpan 10, 20, 30, 40, 50
"$HeaderFmt" -f "TimeSpan( 10, 20, 30, 40, 50 )"
ShowTimeSpanProperties($ts)
# This TimeSpan has all fields overflowing.
$ts = New-Object System.Timespan 1111, 2222, 3333, 4444, 5555
"$HeaderFmt" -f "TimeSpan( 1111, 2222, 3333, 4444, 5555 )"
ShowTimeSpanProperties($ts)
# This TimeSpan is based on a number of days.
$ts = [system.TimeSpan] ([system.TimeSpan]::FromDays(20.8474560))
"$headerFmt" -f "FromDays( 20.84745602 )"
ShowTimeSpanProperties($ts)
# End of script
Subscribe to:
Posts (Atom)