CodeWars之C#0007

输入是总秒数,一年按365天计算,一天按24小时计算

 // formatDuration(62)    // returns "1 minute and 2 seconds"
    //formatDuration(3662)  // returns "1 hour, 1 minute and 2 seconds"

  //formatDuration(0)  // returns "Now"

 string Get(int seconds)
    {
        int year = seconds / 31536000;//得到年        
        int day = (seconds - 31536000 * year) / 86400;//减去完整的年的倍数 得到天
        int hour = (seconds - 31536000 * year - 86400 * day) / 3600;//总时间-年数-天数 
        int minute = (seconds - 31536000 * year - 86400 * day - 3600 * hour) / 60;
        int second = seconds - 31536000 * year - 86400 * day - 3600 * hour - 60 * minute;
        Debug.Log(string.Format("总时间: year:{0}   day:{1}   hour:{2}  minute:{3}   second:{4}", year, day, hour, minute, second));
        string result = "";
        if (second>=1)
        {
            result = second == 1 ? ("1 second") : (second.ToString() + " seconds");
        }
        if (minute>=1)
        {
            if (result=="")
            {
                result = minute == 1 ? ("1 minute") : (minute.ToString() + " minutes");
            }
            else
            {
                result = (minute == 1 ? ("1 minute") : (minute.ToString() + " minutes"))+" and " + result;
            }
        }
        if (hour>=1)
        {
            if (result == "")
            {
                result = hour == 1 ? ("1 hour") : (hour.ToString() + " hours");
            }
            else
            {
                if (result.Contains("and"))
                {
                    result = (hour == 1 ? ("1 hour") : (hour.ToString() + " hours")) + ", " + result;
                }
                else
                {
                    result = (hour == 1 ? ("1 hour") : (hour.ToString() + " hours")) + " and " + result;
                }
               
            }
        }
        if (day >= 1)
        {
            if (result == "")
            {
                result = day == 1 ? ("1 day") : (day.ToString() + " days");
            }
            else
            {
                if (result.Contains("and"))
                {
                    result = (day == 1 ? ("1 day") : (day.ToString() + " days")) + ", " + result;
                }
                else
                {
                    result = (day == 1 ? ("1 day") : (day.ToString() + " days")) + " and " + result;
                }

            }
        }
        if (year >= 1)
        {
            if (result == "")
            {
                result = year == 1 ? ("1 year") : (year.ToString() + " years");
            }
            else
            {
                if (result.Contains("and"))
                {
                    result = (year == 1 ? ("1 year") : (year.ToString() + " years")) + ", " + result;
                }
                else
                {
                    result = (year == 1 ? ("1 year") : (year.ToString() + " years")) + " and " + result;
                }
            }
        }
        if (result=="")
        {
            result = "now";
        }
        return result;
  

    }


    //1分钟60s  1小时3600s   一天86400s   一年31536000
    //,一年是365天,一天是24小时。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值