April 25th, 2006

Days till Xmas

PowerShell Team
PowerShell Team

My daughter loves christmas.  She often asks me, “how long is it till christmas?”  The problem with that is that I’m one of those people that can barely remember what year it is much less the date.  Well, it is one thing to be a flawed person and its another thing to disappoint your daughter.  Monad to the rescue!

Here is a little date math routine I wrote to help me out:

function tillXmas ()
{
    $now = [DateTime]::Now
    [Datetime](“12/25/” + $now.Year) – $Now
}

MSH> tillxmas


Days              : 321
Hours             : 18
Minutes           : 8
Seconds           : 26
Milliseconds      : 171
Ticks             : 277997061718750
TotalDays         : 321.755858470775
TotalHours        : 7722.14060329861
TotalMinutes      : 463328.436197917
TotalSeconds      : 27799706.171875
TotalMilliseconds : 27799706171.875

Thanks to Monad, I can tell my daughter how many seconds to go till Xmas!  Now if I can only get her to stop asking me in the car.

Here is an exercise for you – calculate what time I ran this example (hint – think “date math” and use the Ticks value)

Enjoy!

Jeffrey P. Snover
Monad Architect

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Category
PowerShell

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.