• 根据日期获取,x岁x月x天


    c#:

     DateTime startDate = new DateTime();
                DateTime endDate = new DateTime();
    
                int age =0;
                int month = 0;
                int day = 0;
             
                if (endDate.Month>startDate.Month)
                {
                    age = endDate.Year - startDate.Year;
                    month = endDate.Month - startDate.Month;
                }
                else 
                {
                    age = endDate.Year - startDate.Year-1;
                    month =12+ endDate.Month - startDate.Month;
                }
    
                if (endDate.Day>startDate.Day)
                {
                    day = endDate.Day - startDate.Day;
                }
                else
                {
                    //上个月天数-startDate.Day+endDate.Day
                    day = (endDate-endDate.AddMonths(-1)).Days  - startDate.Day+endDate.Day;
                    month--;
                }
                Response.Write(string.Format("{0}岁{1}月{2}天", age, month, day));

    php:

            $age=0;
            $month=0;
            $day=0;
            
            $startDate=strtotime("2010-09-08 07:06:05");
            $endDate=strtotime("2015-06-05 07:06:05");
            if(date('n',$endDate)>date('n',$startDate)){
                $age=date('Y',$endDate)-date('Y',$startDate);
                $month=date('n',$endDate)-date('n',$startDate);
            }else{
                $age=date('Y',$endDate)-date('Y',$startDate)-1;
                $month=12+date('n',$endDate)-date('n',$startDate);
            }
            
            if(date('j',$endDate)>date('j',$startDate)){
                $day = date('j',$endDate)-date('j',$startDate);
            }else{
                $day= date('t',strtotime('-1 Month',$endDate))-date('j',$startDate)+date('j',$endDate);
                $month--;
            }        
            echo  sprintf('%s岁%s月%s天',$age,$month,$day);

    java:

     Calendar startDate = Calendar.getInstance();
               startDate.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("2010-09-08"));
               
               Calendar endDate = Calendar.getInstance();
               endDate.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("2010-10-5"));
               
               int age =0;
               int month = 0;
               int day = 0;
               if(endDate.get(Calendar.MONTH)>startDate.get(Calendar.MONTH)){
                   age = endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR);
                month = endDate.get(Calendar.MONTH)-startDate.get(Calendar.MONTH);               
               }else{
                   age = endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR)-1;
                month =12+ endDate.get(Calendar.MONTH)-startDate.get(Calendar.MONTH);               
               }
               
               
               if(endDate.get(Calendar.DAY_OF_MONTH)>startDate.get(Calendar.DAY_OF_MONTH)){
                   day = endDate.get(Calendar.DAY_OF_MONTH)-startDate.get(Calendar.DAY_OF_MONTH);           
               }else{
                   Calendar preEndDate = Calendar.getInstance();  
                   preEndDate.set(Calendar.YEAR, endDate.get(Calendar.YEAR));  
                   preEndDate.set(Calendar.MONTH, endDate.get(Calendar.MONTH)-1);  
                   preEndDate.set(Calendar.DATE, 1);
                   preEndDate.roll(Calendar.DATE, -1);
                   int preMonthDays = preEndDate.get(Calendar.DATE);  //获取endDay 上个月的天数
                   
                   day =preMonthDays+endDate.get(Calendar.DAY_OF_MONTH)-startDate.get(Calendar.DAY_OF_MONTH);
                   month--;           
               }
               
               System.out.printf("%s岁%s月%s天",age,month,day);

     

  • 相关阅读:
    69.广搜练习:  最少转弯问题(TURN)
    51..分治算法练习:  4378 【Laoguo】循环比赛
    50.分治算法练习:  二分算法:  2703 奶牛代理商 XII
    [转载]双向广搜
    49.分治算法练习:  1497 取余运算
    48.贪心练习:  1621 混合牛奶
    47..贪心  失恋28天-追女孩篇
    46.贪心算法练习:  区间合并
    45.分支算法练习:  7622:求排列的逆序数
    44.分治算法练习:  一元三次方程求解
  • 原文地址:https://www.cnblogs.com/zhudongchang/p/4643299.html
Copyright © 2020-2023  润新知