简体   繁体   English

使用javascript设置失效日期

[英]Set Expire Date with javascript

I have a form with inputs and I want on submit button click to take the current time and add 10 hours to update the table cell expdate I have this function but doesn't work 我有一个带有输入的表单,我想单击“提交”按钮以获取当前时间并添加10小时来更新表格单元格过期,但我有此功能,但无法正常工作

<script>
$("#registerButton").click(function() {
    var signintime =$("#scantime").val();
    var id = $("#id").val();
    var expdate = ($("#scantime").val() +36000));
       $.ajax({
            url: "updatescan.php",
            method: "POST",
       data: {signintime:signintime,expdate:expdate,id:id} , 
dataType:"text",   
            success: function(data)
                        {                   
                        }
        });
    });
</script>

The field with the id #scantime has a real time clock as a value ID为#scantime的字段具有一个实时时钟作为值

I have also tried this but it doesn't work with javascript 我也尝试过此方法,但不适用于javascript

$expdate = date('Y-m-d H:i:s', time()+36000);
var expdate = date('Y-m-d H:i:s', time()+36000);`

i've managed to get current time and insert to db with this 我已经设法获取当前时间并以此插入数据库

var x = new Date();
var x1=x.getFullYear() + "-" + x.getMonth() + "-" + x.getDate();
x1 = x1 + "  " + x.getHours( )+ ":" + x.getMinutes() + ":" + x.getSeconds();
var expdate=x1;

but how i add 10 hours to this ? 但是我要如何增加10个小时呢? Thank you 谢谢

Please pay attention that getMonth is month-1 请注意,getMonth是第1个月

So if you need dates as a string you should use getMonth()+1 因此,如果您需要日期作为字符串,则应使用getMonth()+ 1

// php
echo strtotime('now +10 hours');

// js 
var d = new Date();
d.setHours(d.getHours()+10);

var d = new Date(); var d = new Date(); d.setHours(10); d.setHours(10); The result of d will be: d的结果将是:

Wed May 25 2016 15:11:44 GMT+0530 (IST). 2016年5月25日星期三15:11:44 GMT + 0530(IST)。

http://www.w3schools.com/jsref/jsref_setHours.asp http://www.w3schools.com/jsref/jsref_setHours.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM