首页 > 解决方案 > 如何在 datepicker 下拉列表中增加 120 天?

问题描述

我有两个日期选择器下拉列表,如果我选择 2020 年 1 月 1 日,我的到期日日期选择器应自动增加 120 天并将到期日设置为 04-30-2020(2020 年 4 月 30 日)。我怎样才能做到这一点,它需要在所有时区和支持的 DST 上工作?我们不允许为此使用任何日期时间库:(。提前感谢您的帮助。

// INCLUDE JQUERY & JQUERY UI 1.12.1

$( function() {
    $( "#datepicker" ).datepicker({
        dateFormat: "dd-mm-yy"
        ,   duration: "fast"
    });
    $( "#datepicker2" ).datepicker({
        dateFormat: "dd-mm-yy"
        ,   duration: "fast"
    });
} );
* {
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
}
body {
    font-family: 'Roboto', sans-serif;
    color: #546E7A;
}
.wrapper {
    max-width: 18rem;
    padding: 0 0.5rem;
    margin-left: auto;
    margin-right: auto;
    padding-top: 4rem;
}
label {
    font-size: 0.75rem;
    font-weight: 400;
    display: block;
    margin-bottom: 0.5rem;
    color: #B0BEC5;
    border: 1px solid #ECEFF1;
    padding: 0.5rem 0.75rem;
    border-radius: 0.5rem;
}
input {
    font-family: 'Roboto', sans-serif;
    display:block;
    border: none;
    border-radius: 0.25rem;
    border: 1px solid transparent;
    line-height: 1.5rem;
    padding: 0;
    font-size: 1rem;
    color: #607D8B;
    width: 100%;
    margin-top: 0.5rem;
}
input:focus {outline: none;}
#ui-datepicker-div {
    display: none;
    background-color: #fff;
    box-shadow: 0 0.125rem 0.5rem rgba(0,0,0,0.1);
    margin-top: 0.25rem;
    border-radius: 0.5rem;
    padding: 0.5rem;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}
.ui-datepicker-calendar thead th {
    padding: 0.25rem 0;
    text-align: center;
    font-size: 0.75rem;
    font-weight: 400;
    color: #78909C;
}
.ui-datepicker-calendar tbody td {
    width: 2.5rem;
    text-align: center;
    padding: 0;
}
.ui-datepicker-calendar tbody td a {
    display: block;
    border-radius: 0.25rem;
    line-height: 2rem;
    transition: 0.3s all;
    color: #546E7A;
    font-size: 0.875rem;
    text-decoration: none;
}
.ui-datepicker-calendar tbody td a:hover {  
    background-color: #E0F2F1;
}
.ui-datepicker-calendar tbody td a.ui-state-active {
    background-color: #009688;
    color: white;
}
.ui-datepicker-header a.ui-corner-all {
    cursor: pointer;
    position: absolute;
    top: 0;
    width: 2rem;
    height: 2rem;
    margin: 0.5rem;
    border-radius: 0.25rem;
    transition: 0.3s all;
}
.ui-datepicker-header a.ui-corner-all:hover {
    background-color: #ECEFF1;
}
.ui-datepicker-header a.ui-datepicker-prev {    
    left: 0;    
    background: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMyIgaGVpZ2h0PSIxMyIgdmlld0JveD0iMCAwIDEzIDEzIj48cGF0aCBmaWxsPSIjNDI0NzcwIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik03LjI4OCA2LjI5NkwzLjIwMiAyLjIxYS43MS43MSAwIDAgMSAuMDA3LS45OTljLjI4LS4yOC43MjUtLjI4Ljk5OS0uMDA3TDguODAzIDUuOGEuNjk1LjY5NSAwIDAgMSAuMjAyLjQ5Ni42OTUuNjk1IDAgMCAxLS4yMDIuNDk3bC00LjU5NSA0LjU5NWEuNzA0LjcwNCAwIDAgMS0xLS4wMDcuNzEuNzEgMCAwIDEtLjAwNi0uOTk5bDQuMDg2LTQuMDg2eiIvPjwvc3ZnPg==");
    background-repeat: no-repeat;
    background-size: 0.5rem;
    background-position: 50%;
    transform: rotate(180deg);
}
.ui-datepicker-header a.ui-datepicker-next {
    right: 0;
    background: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMyIgaGVpZ2h0PSIxMyIgdmlld0JveD0iMCAwIDEzIDEzIj48cGF0aCBmaWxsPSIjNDI0NzcwIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik03LjI4OCA2LjI5NkwzLjIwMiAyLjIxYS43MS43MSAwIDAgMSAuMDA3LS45OTljLjI4LS4yOC43MjUtLjI4Ljk5OS0uMDA3TDguODAzIDUuOGEuNjk1LjY5NSAwIDAgMSAuMjAyLjQ5Ni42OTUuNjk1IDAgMCAxLS4yMDIuNDk3bC00LjU5NSA0LjU5NWEuNzA0LjcwNCAwIDAgMS0xLS4wMDcuNzEuNzEgMCAwIDEtLjAwNi0uOTk5bDQuMDg2LTQuMDg2eiIvPjwvc3ZnPg==');
    background-repeat: no-repeat;
    background-size: 10px;
    background-position: 50%;
}
.ui-datepicker-header a>span {
    display: none;
}
.ui-datepicker-title {
    text-align: center;
    line-height: 2rem;
    margin-bottom: 0.25rem;
    font-size: 0.875rem;
    font-weight: 500;
    padding-bottom: 0.25rem;
}
.ui-datepicker-week-col {
    color: #78909C;
    font-weight: 400;
    font-size: 0.75rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script
  src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
  integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
  crossorigin="anonymous"></script>
<div class="wrapper">
    <label for="datepicker">Start Date
        <input type="date" id="datepicker" autocomplete="off">
    </label>    
        <label for="datepicker">Due Date
        <input type="date" id="datepicker2" autocomplete="off">
    </label>    

</div>
<div><p></div>

标签: javascriptjquerydatetimejquery-uitimezone

解决方案


给定一个日期字段:

<input id="dt" type="date" />

将其设置为从现在起 120 天:

var date = new Date();
date.setDate(date.getDate() + 120);
document.getElementById("dt").value = date;

推荐阅读