Use javascript to control the valid time and display the time to expire

  • 2020-03-30 01:10:38
  • OfStack

JavaScript implementation of a set of time period of validity, I use jQuery to achieve the feeling of the code is too not concise, this is a piece of js code written by colleagues, their own study


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Headless document </title>
<style type="text/css">
#lastdate{ color:#FF9900; font-style:normal;}
</style>
</head>
<body>
<div class="f-c">
          <select id="date_sel" name="period" class="buyConQc select150">
                           <option value="0"> Within a day </option>
                           <option value="1"> In two days. </option>
                           <option value="2"> Within three days </option>
                           <option value="3"> In four days </option>

          </select>
        <span class="postAction"> in   <em id="lastdate">2012-9-28</em>   After will expire </span>

        </div>
 <script type="text/javascript">
    document.getElementById('date_sel').onchange = function(){
        var time = new Date();
        var pre_year = time.getFullYear();
        var pre_month = time.getMonth();
        var pre_day = time.getDate();
        var sel_day = parseInt(document.getElementById('date_sel').value);
        switch(sel_day){
            case 0: pre_day +=1;break;
            case 1: pre_day +=2;break;
            case 2: pre_day +=3;break;
            case 3: pre_day +=4;break;
            default:pre_day +=1;break;
}
    var endtime = new Date(pre_year, pre_month, pre_day);
    document.getElementById('lastdate').innerHTML = endtime.getFullYear()+"-"+(endtime.getMonth()+1)+"-"+endtime.getDate();
}; 
</script>
</body>
</html>


Related articles: