使用Powershell计算自己活了多少天,以及反推自己或别人的生日

#1、某80后的生日是1980年10月1日,推算其到某一天活了几天
$HisBirthdayDay  = "10/1/1980"


#1.1 他到当前计算日期活了多少天?

$HisLivingDays = ([datetime]::Now - [datetime]$HisBirthdayDay).Days


#1.2  他到今年国庆节活了几天?
$ToNationalDay = ([datetime]"10/1/2009"- [datetime]$HisBirthdayDay).Days

#结果是$ToNationalDay  =  9131 (天)

 

 

#2、已知某人到2009年国庆节生活了9131天,推算他的生日

#已知的天数9131天,计算日期是2009年10月1日

$ToNationalDay = 9131
$CalcDay = "10/1/2009"

$BirthdayYear = ([datetime]$CalcDay).Year - [System.Math]::Round($ToNationalDay / 365)

$LeftDays = ([datetime]$CalcDay - [datetime]"1/1/$BirthdayYear").Days - $ToNationalDay

$StartMonth = [System.Math]::Floor($LeftDays / 30)

$NewLeftDays = ([datetime]$CalcDay - [datetime]"$StartMonth/1/$BirthdayYear").Days - $ToNationalDay


function ModifyDate1()
{
    if(($NewLeftDays - 30) -ge 0)
    {
 
         $DayofMonth = $NewLeftDays - 29
       
         Return "$($StartMonth + 1)" + " - " + "$DayofMonth"
    }
    else
    {
        Return "$StartMonth" + " - " + "$NewLeftDays"
    }
}
function ModifyDate2()
{
    if(($NewLeftDays - 31) -ge 0)
    {
 
         $DayofMonth = $NewLeftDays - 30
       
          Return "$($StartMonth + 1)" + " - " + "$DayofMonth"
    }
    else
    {
        Return "$StartMonth" + " - " + "$NewLeftDays"
    }
}
function ModifyDate3()
{
#考虑是否闰年   
    if(($BirthdayYear/4) -eq 0)
    {

         if(($NewLeftDays - 29) -ge 0)
         {
 
             $DayofMonth = $NewLeftDays - 28
       
             Return "$($StartMonth + 1)" + " - " + "$DayofMonth"
        }
        else
        {
            Return "$StartMonth" + " - " + "$NewLeftDays"
        }
    }
    else
    {
        if(($NewLeftDays - 28) -ge 0)
        {
 
             $DayofMonth = $NewLeftDays - 27
       
             Return "$($StartMonth + 1)" + " - " + "$DayofMonth"
        }
        else
        {
            Return "$StartMonth" + " - " + "$NewLeftDays"
        }
    }
}
switch($StartMonth)
{
1 {$BirthdayMonth_Day = ModifyDate2;break}
2 {$BirthdayMonth_Day = ModifyDate3;break}
3 {$BirthdayMonth_Day = ModifyDate2;break}
4 {$BirthdayMonth_Day = ModifyDate1;break}
5 {$BirthdayMonth_Day = ModifyDate2;break}
6 {$BirthdayMonth_Day = ModifyDate1;break}
7 {$BirthdayMonth_Day = ModifyDate2;break}
8 {$BirthdayMonth_Day = ModifyDate2;break}
9 {$BirthdayMonth_Day = ModifyDate1;break}
10 {$BirthdayMonth_Day = ModifyDate2;break}
11 {$BirthdayMonth_Day = ModifyDate1;break}
12 {$BirthdayMonth_Day = ModifyDate2;break}
}


Echo “your birthday is $BirthdayYear - $BirthdayMonth_Day”

 

 

 

posted on 2009-09-08 14:11  曾令理  阅读(2966)  评论(2编辑  收藏  举报

导航