JavaScript to cnvert days to be month and day mode

This is a small tool to convert a number which refer to days from the begining of this year till today to be month & day mode.

For example: reformat 123 to Month 5 day 3.

/*
* Copyright : Bian Junjie
*/
<!DOCTYPE html>
<html>
<head>

<style type="text/css">
  body {font-size:9px}
  td, th {height:15px; width:50px}
  table {width:500px; border-collapse:collapse }
  table,td { border: 1px solid blue}
</style>

<script type="text/javascript"
src="Common_JS.js">
</script>

</head>
<body>

<table>
  <tr>
    <th>Days</th>
    <th>Date</th>
    <th><input type='button' value='Calc' οnclick="calcFunction()"/></th>
  </tr>
  <tr>
    <td><input id='y_days' type='text' width=50px οnkeypress="keyProcess(event)"/></td>
    <td id='date'> </td>
    <td> </td>
  </tr>
    <tr>
      <td colspan="3" id="outMsg"> </td>
    </tr>

</table>

</body>


  <script type="text/javascript">
   var dayArray=[31,28,31,30,31,30,31,31,30,31,30,31];
//   var dayArray=[31,29,31,30,31,30,31,31,30,31,30,31];

function keyProcess(e){
	if(e.keyCode == 13)
	  calcFunction();
}

   function calcFunction(){
      var i=0;
      dayInput = commFunction.getValue("y_days");
     if(1 == commFunction.empCheck(dayInput)){
        commFunction.writeMsg("outMsg",commFunction.EMPTYVALUE);
        return;
     }
     if(1 == commFunction.NumCheck(dayInput)){
        commFunction.writeMsg("outMsg",commFunction.NONNUM);
        return;
     }

      do{
         if(i>=12 || dayInput<=dayArray[i]) break;
         dayInput -= dayArray[i++];
      }while(true);

    if(i>=12 && dayInput >0){
    	commFunction.writeMsg("outMsg","too many days inputted!");
    	return;
    }
      writeDate(i+1,dayInput);
   }

   function writeDate(p1,p2){
      commFunction.writeMsg("outMsg","");
      document.getElementById("date").innerHTML = "Month  " + p1.toString() + "   Day  " + p2.toString();
   }

</script>
</html>


Outer Javascript:

// Copyright: Bian Junjie
//This is collection with common useful functions

  var commFunction = {
        EMPTYVALUE:"输入不能为空",
        NONNUM:"不能输入非数字",
        empCheck: function(InputValue){
                    if(InputValue.length==0 || InputValue.length == undefined){
                        return 1;
                    }else{ return 0;}
                  }
        ,
        NumCheck: function (InputValue){
                    if(isNaN(parseInt(InputValue))){
                              return 1;
                    }else
                     return 0;
                   }
        ,
        getValue: function(id){
                     return document.getElementById(id).value;
                  }
        ,
        writeMsg: function(id,Msg){
                     document.getElementById(id).innerHTML = Msg;
                  } 
        ,
        Zerotrim: function(InputValue){
                     for(var i= 0;;){
                        if(parseInt(InputValue.charAt(i)) == 0)
                            InputValue = InputValue.substr(i+1);
                        if(parseInt(InputValue.charAt(i)) != 0)
	                    break;
                     }
                    // input like '000' will be empty value after trim
                    // give default value make sure no empty value return

                    if(InputValue.length == 0) 
                       InputValue = "0";

                    return InputValue;
                  }
  }

Result:

IE 11, cannot resolve the outer JavaScript referece. Chrome & 360IE works fine.



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值