在JS中减去日期

时间:2013-06-25 14:52:54

标签: javascript jquery

这是我的表格:

<form action="" method="POST"> <input type='hidden' name='csrfmiddlewaretoken' value='6FPkks049G8IWRkLW6mptEKGRwVevsVo' />
    <label for="id_date">Date:</label></th><td><input class="vDateField" id="id_date" name="date" size="10" type="text" />

    <label for="id_rday">How many days before?</label></th><td>
    <select id="id_rday" name="rday">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7" selected="selected">7</option>
    </select>

    <label for="id_remind_at">Reminder date:</label></th><td><input id="id_remind_at" name="remind_at" type="text" />

    <input type="submit" value="Add"/>
</form>

如何创建这样的东西:

如果用户选择id_dateid_rdate,则会自动填写字段id_reminder

`id_reminder` = `id_day - id_rday`

1 个答案:

答案 0 :(得分:1)

您可以从Date对象获取该月的某一天,然后减去:

var today = new Date();
var startDate = new Date();
startDate.setDate(today.getDate() - 5);  // 5 days ago

http://www.w3schools.com/js/js_obj_date.asp