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" "">
<html xmlns="">
<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;}
<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>

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

 <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);
            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();

Related articles: