- <#
- .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
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.datetime. Show all posts
Showing posts with label system.datetime. Show all posts
Tuesday, 8 November 2011
Show-TimeSpan.ps1
Labels:
Power,
PowerShell scripts,
system.datetime,
System.TImespan
Monday, 2 August 2010
Get-ConvertedTime.ps1
- <#
- .SYNOPSIS
- This script converts time using ConvertTime method.
- .DESCRIPTION
- This script re-implements an MSDN sample.
- .NOTES
- File Name : Get-ConvertedTime.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/08/get-convertedtimeps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/bb382835.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-ConvertedTime.ps1
- Local time zone: (GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London
- Converted 1/1/2010 12:01:00 AM Unspecified to 12/31/2009 07:01:00 PM
- Converted 1/1/2010 12:01:00 AM Utc to 12/31/2009 07:01:00 PM
- Converted 1/1/2010 12:01:00 AM Local to 12/31/2009 07:01:00 PM
- Converted 11/6/2010 11:30:00 PM Unspecified to 11/6/2010 07:30:00 PM
- Converted 11/7/2010 02:30:00 AM Unspecified to 11/6/2010 10:30:00 PM
- #>
- #Create an array of times to convert
- [DateTime[]] $times = (New-Object system.dateTime 2010, 1, 1, 0, 1, 0),
- (New-Object system.DateTime(2010, 1, 1, 0, 1, 0, [System.DateTimeKind]::Utc)),
- (New-Object system.dateTime(2010, 1, 1, 0, 1, 0, [System.DateTimeKind]::Local)),
- (New-Object system.DateTime(2010, 11, 6, 23, 30, 0)),
- (New-Object system.DateTime(2010, 11, 7, 2, 30, 0) );
- # Retrieve the time zone for Eastern Standard Time (U.S. and Canada).
- try {
- $Est = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time");
- }
- catch {
- "Unable to retrieve the Eastern Standard time zone."
- return;
- }
- # Display the current time zone name.
- "Local time zone: {0}`n" -f [system.TimeZoneInfo]::Local.DisplayName
- # Convert and display each time in the $times array
- foreach ($timeToConvert in $times) {
- $TargetTime = [System.TimeZoneInfo]::ConvertTime($TimeToConvert, $Est)
- "Converted {0} {1} to {2}" -f $timeToConvert,$timeToConvert.Kind, $targetTime
- }
Labels:
ConvertTime,
powershell,
PowerShell scripts,
system.datetime
Wednesday, 5 August 2009
Get-Thursday
- <#
- .SYNOPSIS
- This script checks of a particular date in the past was a Thurday.
- .DESCRIPTION
- This script creates a DateTime object set for 1st May, 2003. The
- script then check to see if that day is a Thursday then displays
- the day of week for that date (which is a Thursday). This script
- is a copy of the MSDN sample, written in PowerShell.
- .NOTES
- File Name : Get-Thursday.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2009/08/get-thursday.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.dayofweek(VS.71).aspx
- .EXAMPLE
- PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.DateTime\get-thursday.ps1'
- Is Thursday the day of the week for 5/1/2003?: True
- The day of the week for 5/1/2003 is Thursday.
- #>
- ##
- # Start of script
- ##
- # Create a DateTime for the first of May, 2003.
- $dt = New-Object System.DateTime 2003, 5, 1
- # Now - is it Thursday?
- "Is Thursday the day of the week for {0:d}?: {1}" -f $dt,($dt.DayOfWeek -eq [system.DayOfWeek]::Thursday)
- "The day of the week for {0:d} is {1}." -f $dt, $dt.DayOfWeek
- # End of Script
Labels:
system.datetime,
system.dayofweek
Thursday, 12 March 2009
Get-DaysInMonth.ps1
- <#
- .SYNOPSIS
- Displys number of days in a particular month.
- .DESCRIPTION
- This scrips uses System.DateTime to determine, then
- display, the numbe of days in some months.
- .NOTES
- File Name : Get-DaysInMonth.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- PSH [C:\foo]: . 'C:\foo\Get-daysinmonth.ps1'
- There are 31 days in July 2001
- There are 28 days in February 1998
- There are 29 days in February 1996
- There are 31 days in August 1950
- #>
- ##
- # Start of script
- ##
- # Create variables representing months
- $July = 7
- $February = 2
- $August = 8
- # daysInJuly should be 31.
- $daysInJuly = [System.DateTime]::DaysInMonth(2001, $July)
- "There are $daysinjuly days in July 2001"
- # DaysInFeb should be 28 because the year 1998 was not a leap year.
- $daysInFeb = [System.DateTime]::DaysInMonth(1998, $February)
- "There are $daysinfeb days in February 1998"
- # daysInFebLeap gets 29 because the year 1996 was a leap year.
- $daysInFebLeap = [System.DateTime]::DaysInMonth(1996, $February)
- "There are $daysinfebleap days in February 1996"
- # An august year indeed.
- $daysInAugust = [System.DateTime]::DaysInMonth(1950, $August)
- "There are $daysinAugust days in August 1950"
- # End of Script
Labels:
DaysInMonth,
powershell,
PowerShell scripts,
system.datetime
Monday, 19 January 2009
Get-LeapYear.ps1
- <#
- .SYNOPSIS
- Demonstrates use of System.Datetime to determine leap year
- .DESCRIPTION
- This script creates four date objects and checks to see if
- the date is a leap year. The last object is today's date.
- .NOTES
- File Name : Get-LeapYear.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- PS c:\foo> .\Get-LeapYear.ps1
- 2000 is a leap year: True
- 2002 is a leap year: False
- 2004 is a leap year: True
- 2009 is a leap year: False
- #>
- ##
- # Start of script
- ##
- # Create three specific date objects, plus today
- $d1 = [system.datetime] "Jan 1 2000"
- $d2 = [System.datetime] "Jan 1 2002"
- $d3 = [System.datetime] "Jan 1 2004"
- $d4 = get-date
- # Are they leap years?
- "{0} is a leap year: {1}" -f $d1.year,([system.datetime]::isleapyear($d1.year))
- "{0} is a leap year: {1}" -f $d2.year,([system.datetime]::isleapyear($d2.year))
- "{0} is a leap year: {1}" -f $d3.year,([system.datetime]::isleapyear($d3.year))
- "{0} is a leap year: {1}" -f $d4.year,([system.datetime]::isleapyear($d4.year))
- # End Script
Labels:
powershell,
PowerShell scripts,
system.datetime
Friday, 2 January 2009
Get-DateTime.ps1
- <#
- .SYNOPSIS
- Uses .NET formatting strings to format date/time values
- .DESCRIPTION
- Recreates an MSDN sample for displaying Date/TIme Format. See
- links section for pointer to original Sample.
- .NOTES
- File Name : Get-DateTime.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .EXAMPLE
- PS C:\foo> .\Get-DateTime.ps1
- FORMAT en-gb EXAMPLE
- CHAR VALUE OF ASSOCIATED PROPERTY, if ANY
- d 12/28/2008
- dd/MM/yyyy (ShortDatePattern)
- D 28 December 2008
- dd MMMM yyyy (LongDatePattern)
- f 28 December 2008 16:37
- F 28 December 2008 16:37:00
- dd MMMM yyyy HH:mm:ss (FullDateTimePattern)
- g 28/12/2008 16:37
- G 28/12/2008 16:37:00
- m 28 December
- dd MMMM (MonthDayPattern)
- M 28 December
- dd MMMM (MonthDayPattern)
- o 2008-12-28T16:37:00.4017900+00:00
- r Sun, 28 Dec 2008 16:37:00 GMT
- ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
- R Sun, 28 Dec 2008 16:37:00 GMT
- ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
- s 2008-12-28T16:37:00
- yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
- t 16:37
- HH:mm (ShortTimePattern)
- T 16:37:00
- HH:mm:ss (LongTimePattern)
- u 2008-12-28 16:37:00Z
- yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
- U 28 December 2008 16:37:00\n
- y December 2008
- MMMM yyyy (YearMonthPattern)
- Y December 2008
- MMMM yyyy (YearMonthPattern)
- .EXAMPLE
- PS C:\foo> Get-Help Get-DateTime.ps1
- Left as an exercise for the reader!
- .LINK
- Script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample Page showing C# original
- http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx
- #>
- ##
- # Start of Script
- ##
- # Create and initialise a DateTimeFormatInfo associated with the en-US culture.
- $culture = [system.Globalization.CultureInfo]::CreateSpecificCulture("en-gb")
- $dtfi = $culture.DateTimeFormat
- # Create a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
- # The Gregorian calendar is the default calendar for the en-GB culture.
- $dt = get-date
- "FORMAT en-gb EXAMPLE"
- "CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY"
- " d {0} " -f $DT.ToString("d", $DI)
- " {0} {1} " -f $dtfi.ShortDatePattern, "(ShortDatePattern)"
- " D {0} " -f $dt.ToString("D", $DTFI)
- " {0} {1} " -f $dtfi.LongDatePattern, "(LongDatePattern)"
- " f {0} " -f $dt.ToString("f", $DTFI)
- " F {0} " -f $dt.ToString("F", $DTFI)
- " {0} {1} " -f $dtfi.FullDateTimePattern, "(FullDateTimePattern)"
- " g {0} " -f $dt.ToString("g", $DTFI)
- " G {0} " -f $dt.ToString("G", $DTFI)
- " m {0} " -f $dt.ToString("m", $DTFI)
- " {0} {1} " -f $dtfi.MonthDayPattern, "(MonthDayPattern)"
- " M {0} " -f $dt.ToString("M", $DTFI)
- " {0} {1} " -f $dtfi.MonthDayPattern, "(MonthDayPattern)"
- " o {0} " -f $dt.ToString("o", $DTFI)
- " r {0} " -f $dt.ToString("r", $DTFI)
- " {0} {1} " -f $dtfi.RFC1123Pattern, "(RFC1123Pattern)"
- " R {0} " -f $dt.ToString("R", $DTFI)
- " {0} {1} " -f $dtfi.RFC1123Pattern, "(RFC1123Pattern)"
- " s {0} " -f $dt.ToString("s", $DTFI)
- " {0} {1} " -f $dtfi.SortableDateTimePattern, "(SortableDateTimePattern)"
- " t {0} " -f $dt.ToString("t", $DTFI)
- " {0} {1} " -f $dtfi.ShortTimePattern, "(ShortTimePattern)"
- " T {0} " -f $dt.ToString("T", $DTFI)
- " {0} {1} " -f $dtfi.LongTimePattern, "(LongTimePattern)"
- " u {0} " -f $dt.ToString("u", $DTFI)
- " {0} {1} " -f $dtfi.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)"
- " U {0}\n " -f $dt.ToString("U", $DTFI)
- " y {0} " -f $dt.ToString("y", $DTFI)
- " {0} {1} " -f $dtfi.YearMonthPattern, "(YearMonthPattern)"
- " Y {0} " -f $dt.ToString("Y", $DTFI)
- " {0} {1} " -f $dtfi.YearMonthPattern, "(YearMonthPattern)"
- # End of Script
Labels:
powershell,
PowerShell scripts,
PowerShell V2,
Script,
scripts,
system.datetime
Subscribe to:
Posts (Atom)