<script language=javascript>
function getDateCn(hourValue,formatStr){ var StandardFormatEn="ymdhfs";
if(formatStr==null) formatStr="ymdhfs";
var StandardFormatCn="年月日时分钞";
var theYmdhfs=new Array();
theSecondValue=(hourValue*3600)%10000000000;
theYmdhfs[5]=theSecondValue%60;//秒
theSecondValue=(theSecondValue-theYmdhfs[5])/60;
theYmdhfs[4]=theSecondValue%60;//分
theSecondValue=(theSecondValue-theYmdhfs[4])/60;
theYmdhfs[3]=theSecondValue%24;//时
theSecondValue=(theSecondValue-theYmdhfs[3])/24;
theYmdhfs[2]=theSecondValue%30;//天
theSecondValue=(theSecondValue-theYmdhfs[2])/30;
theYmdhfs[1]=theSecondValue%12;//月
theSecondValue=(theSecondValue-theYmdhfs[1])/12;
theYmdhfs[0]=theSecondValue%60;//年
var returnStr="";
for(var i=0;i<formatStr.length;i++)
{
returnStr+=theYmdhfs[StandardFormatEn.indexOf(formatStr.substr(i,1))]+StandardFormatCn.substr(StandardFormatEn.indexOf(formatStr.substr(i,1)),1);
}
return returnStr;
}var theHours=24.36;
alert(getDateCn(theHours));
</script>

解决方案 »

  1.   

    非常谢谢 JK_10000(JK1) 
    但是还有一个很关键的问题,
    如果 value=24*60+ 12 且 format ='ymd'
    那么返回应该是 0年2月0.5天 (而不是0年2月0天)即最前面的单位元素要保证是整数,最后一位的单元元素可为小数(最多两位)
    如果只有一个单位即value=24*60+ 12 且 format ='m',则 要返回 2.01月(即2+[0.5/30])不知道如何解决这个问题?
      

  2.   

    <script language=javascript>
    function getDateCn(hourValue,formatStr){
    var theIndex;
    var StandardFormatEn="ymdhfs";
    if(formatStr==null||formatStr=="") formatStr="ymdhfs";
    var StandardFormatCn="年月日时分钞";
    var rateToMilliSecond=new Array(365*24*3600000,30*24*3600000,24*3600000,3600000,60000,1000)
    theMilliSecondValue=hourValue*3600000;
    var returnStr="";
    for(var i=0;i<formatStr.length-1;i++)
    {
    theIndex=StandardFormatEn.indexOf(formatStr.substr(i,1));
    returnStr+=(theMilliSecondValue-theMilliSecondValue%rateToMilliSecond[theIndex])/rateToMilliSecond[theIndex]+StandardFormatCn.substr(theIndex,1);
    theMilliSecondValue%=rateToMilliSecond[theIndex];
    }
    theIndex=StandardFormatEn.indexOf(formatStr.substr(formatStr.length-1,1));
    returnStr+=Math.round(theMilliSecondValue/rateToMilliSecond[theIndex])+StandardFormatCn.substr(theIndex,1);
    return returnStr;
    }var theHours=7225.55;
    alert(getDateCn(theHours,"dh"));
    alert(getDateCn(theHours));
    </script>
      

  3.   

    如果只有一个单位即value=24*60+ 12 且 format ='m',则 要返回 2.01月(即2+[0.5/30])还是不行!
      

  4.   


    <script language=javascript>
    function getDateCn(hourValue,formatStr){
    var theIndex;
    var StandardFormatEn="ymdhfs";
    if(formatStr==null||formatStr=="") formatStr="ymdhfs";
    var StandardFormatCn="年月日时分钞";
    var rateToMilliSecond=new Array(365*24*3600000,30*24*3600000,24*3600000,3600000,60000,1000)
    theMilliSecondValue=hourValue*3600000;
    var returnStr="";
    for(var i=0;i<formatStr.length-1;i++)
    {
    theIndex=StandardFormatEn.indexOf(formatStr.substr(i,1));
    returnStr+=(theMilliSecondValue-theMilliSecondValue%rateToMilliSecond[theIndex])/rateToMilliSecond[theIndex]+StandardFormatCn.substr(theIndex,1);
    theMilliSecondValue%=rateToMilliSecond[theIndex];
    }
    theIndex=StandardFormatEn.indexOf(formatStr.substr(formatStr.length-1,1));
    returnStr+=theMilliSecondValue/rateToMilliSecond[theIndex]+StandardFormatCn.substr(theIndex,1);
    return returnStr;
    }var theHours=24*60+12;
    alert(getDateCn(theHours,"m"));
    alert(getDateCn(theHours));
    </script>