【发布时间】:2020-10-28 19:01:25
【问题描述】:
我有两个日期选择器下拉菜单,如果我选择 2020 年 1 月 1 日,我的截止日期选择器应该自动增加 120 天并将截止日期设置为 2020 年 4 月 30 日(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>
【问题讨论】:
-
你知道在 html 5 浏览器中 会在点击时弹出一个日期,对吧?那 type="time" 和 type="datetime" 同样会显示这些控件?
-
@ControlAltDel 我刚刚更改为 HTML5 日期时间选择器,而不是 jquery 之一。谢谢 !你能帮我如何将截止日期设置为比当前选择的日期提前 120 天吗?
-
我不愿建议您删除此问题,因为我太快将其推荐给其他没有经验的用户。但是如果我的建议解决了你的问题,你可能要考虑删除这个帖子,因为问题解决了,但原来的问题没有得到回答。
-
这能回答你的问题吗? Adding days to given date in jQuery
-
@ControlAltDel 我的问题还没有解决。我需要在到期日提前 120 天设置。我该怎么做?请多多指教。
标签: javascript jquery datetime jquery-ui timezone