如何计算另一个日期的剩余天数

时间:2016-04-23 23:23:05

标签: python openerp odoo-8

如何从视图导入日期并计算另一个日期的剩余天数?

这是我尝试的解决方案,但它无法正常工作:

<html>

<body>
  <section>
    <div>
        <canvas id="canvas" width="700" height="300"></canvas>
    </div>

    <script type="text/javascript">
    var canvas,
        context,
    x = Math.floor((Math.random() * 400) + 1),          //Ball x coordinate
    y = Math.floor((Math.random() * 300) + 1),          //Ball y coordinate
    dx = Math.floor((Math.random() * 10) + 1),              //X velocity
    dy = Math.floor((Math.random() * 4) + 1),               //Y velocity
    width = 700,        //Width of the background
    height = 300;       //Height of the background


function drawCircle(x,y,r) {  //Draws the ball
  context.beginPath();
  context.arc(x, y, r, 0, Math.PI*2, true);
  context.fill();
}


function drawRect(x,y,w,h) { //Draws the background
  context.beginPath();
  context.rect(x,y,w,h);
  context.closePath();
  context.fill();
}

function start() { //Runs when the window loads. Stores canvas and context      in variables. Runs "draw" on an interval of 10ms
  canvas = document.getElementById("canvas");
  context = canvas.getContext("2d");
  return setInterval(draw, 10);
}

function drawSquare(){
var canvas = document.getElementById('canvas');
  if (canvas.getContext){
    var ctx = canvas.getContext('2d');
    ctx.fillStyle = "white";
    ctx.beginPath();
    ctx.moveTo(200,50);
    ctx.lineTo(250,50);
    ctx.lineTo(300, 100);
    ctx.lineTo(250,25);
    ctx.fill();



}

}



 function drawTri(){
 var canvas = document.getElementById('canvas');
  if (canvas.getContext){
    var ctx = canvas.getContext('2d');

    ctx.beginPath();
    ctx.moveTo(75,50);
    ctx.lineTo(100,75);
    ctx.lineTo(75,25);
    ctx.fill();
  }

}



function draw() {
  context.clearRect(0, 0, width, height); //Clears the drawing space
  context.fillStyle = "black";            //Sets fillstyle to black (for the     background)
  drawRect(0,0, width, height);           //Draws the background
  context.fillStyle = "white";            //Sets fillstyle to white (for the     ball)
  drawCircle(x, y, 10); 
drawTri(); 
drawSquare(); //Calls function to draw the ball
  if (x + dx > width || x + dx < 0)       //If the ball would leave the     width (right or left) on the next redraw...
    dx = -dx;                             //reverse the ball's velocity
  if (y + dy > height || y + dy < 0)      //If the ball would leave the     height (top or bottom) on the next redraw...
    dy = -dy;                             //reverse the ball's velocity
  x += dx;                                //Increase ball x speed by     velocity
  y += dy;                                //Increase ball y speed by     velocity
}           
window.onload = start;                    //Run "start" function after the     window loads
</script>

  </section>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

你必须正确地做到这一点:

  • 指定正确的日期/时间格式

  • 从字符串

  • 解析日期时间
  • 减去相同的数据类型:datetime - datetime

代码:

In [68]: current_date = '29/04/2016 02:02:02'

In [69]: date_format = '%d/%m/%Y %H:%M:%S'

In [70]: (datetime.datetime.strptime(current_date, date_format) - datetime.datetime.now()).days
Out[70]: 5