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.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章