Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何根据用户的选择使用jQuery进行时间戳_Javascript_Jquery - Fatal编程技术网

Javascript 如何根据用户的选择使用jQuery进行时间戳

Javascript 如何根据用户的选择使用jQuery进行时间戳,javascript,jquery,Javascript,Jquery,我试图根据用户的选择生成一个时间戳 我有一个输入值为空的表单: <input type="hidden" name="sub_enddate" value=" " /> 我需要根据用户选择的未来特定时间值,用时间戳填充空值: <select name="Membership Duration"> <option value="">Select A Membership Duration</option> <option class="6"

我试图根据用户的选择生成一个时间戳

我有一个输入值为空的表单:

<input type="hidden" name="sub_enddate" value=" " />
我需要根据用户选择的未来特定时间值,用时间戳填充空值:

<select name="Membership Duration">
<option value="">Select A Membership Duration</option>
<option class="6" value="6 Months">6 Month Membership</option>
<option class="3" value="3 Months">3 Month Membership</option>
<option class="1" value="1 Months">1 Month Membership</option>
</select>
格式如下:20110325 2011-03-25。因此,如果用户从下拉菜单中选择:

6个月成员身份隐藏输入字段中的值为:20110925从今天起的6个月后,2011年3月25日。 3个月成员身份隐藏输入字段中的值为:20110625从今天起3个月后,2011年3月25日。 1个月成员身份隐藏输入字段中的值为:20110425从今天起,2011年3月25日,未来1个月。

这是一个好办法:

$('#mem_dur').change(function(e) {
    var tVal = $(this).find('option:selected').val()[0],
        tDate = new Date(),
        mDate, mDateMS, mDateStr
        oneMonth = 31 * 86400000;

    mDateMS = tDate.getTime() + tVal * oneMonth;
    mDate = new Date(mDateMS);
    mDateStr = String(mDate.getFullYear()) + 
               ((mDate.getMonth() < 9) ? "0" + (mDate.getMonth() + 1) : mDate.getMonth() + 1) + 
               ((mDate.getDate() < 10) ? "0" + mDate.getDate() : mDate.getDate());

    mDateStr = (isNaN(parseInt(mDateStr,10))) ? "" : mDateStr;

    $('#sub_enddate').val(mDateStr);
});
基本上,setMonth和getMonth函数是最重要的

这是我编的:


new Date.setMonthnew Date.getMonth+6将返回从现在开始的未来6个月的时间戳。然后你可以将它格式化成你想要的格式。嘿,pimvdb,谢谢你的回复。我有点理解你的意思,但在实际实现上有点不知所措。你需要expiration.getMonth<9,因为你要给值加1。否则,干得好!嗯…这把Nannan放在输入字段的值中…不知道为什么。这行得通…thnx!我唯一要问你的问题是:在6个月后,它加上一个月的第2天,即今天是25号,它输出27,在3个月后,它加上一个月的第1天,即今天是25号,它输出26。知道为什么吗?理想情况下,我希望它保持在同一天,即在本例中,以今天的日期为基础25天。我的将月份转换为毫秒,并将所有月份视为31天,因此增加了天数。如果您不想给您的会员几天的免费时间,我会使用使用setMonth的解决方案之一。
$('#mem_dur').change(function(e) {
    var tVal = $(this).find('option:selected').val()[0],
        tDate = new Date(),
        mDate, mDateMS, mDateStr
        oneMonth = 31 * 86400000;

    mDateMS = tDate.getTime() + tVal * oneMonth;
    mDate = new Date(mDateMS);
    mDateStr = String(mDate.getFullYear()) + 
               ((mDate.getMonth() < 9) ? "0" + (mDate.getMonth() + 1) : mDate.getMonth() + 1) + 
               ((mDate.getDate() < 10) ? "0" + mDate.getDate() : mDate.getDate());

    mDateStr = (isNaN(parseInt(mDateStr,10))) ? "" : mDateStr;

    $('#sub_enddate').val(mDateStr);
});
Number.prototype.fillUpWithZeroes = function() {
    // function to make a number like 3 formatted to 03.
    return (this + "").length === 1 ? "0" + this : "" + this;
}

$('select').change(function() {
    var time_now = new Date; // current time
    var chosen_value = $(this).val(); // chosen amount of months
    var time_in_future = new Date( // construct future time using value
                          time_now.setMonth(
                           time_now.getMonth()
                           + (chosen_value-0)
                          )
                         );

    var year = time_in_future.getFullYear(); // format everything
    var month = (time_in_future.getMonth() + 1).fillUpWithZeroes();
                 // Month starts at 0 (January)! So add 1.
    var date = time_in_future.getDate().fillUpWithZeroes();
    var formatted = year + month + date;

    $('input').val(formatted);
});