这是我的代码,
function getDateonperiod(elem)
{
var periodval=$("#period"+elem).val();
var days = periodval * 30;
var startDate=new Date();
var endDate = new Date();
endDate.setDate(endDate.getDate() + days);
var strDate = startDate.getFullYear() + "-" + (startDate.getMonth()+1) + "-" + startDate.getDate();
$("#from_date"+elem).val(strDate);
$("#to_date"+elem).val((endDate.getFullYear()+'-'+endDate.getMonth() + 1)+'-'+endDate.getDate());
}
更改输入字段值获取天数days
。我正确地获取了开始日期,但是如果输入值1工作正常,则结束日期,但是从当前日期开始,它会返回多个2016-11-27
2016-21-28
。如何解决这个问题?
答案 0 :(得分:1)
你错过了一个括号:
endDate.getFullYear()+'-'+(endDate.getMonth() + 1)+'-'+endDate.getDate();
试试这个:
$( "#period" ).change(function() {
var periodval=$("#period").val();
var days = periodval * 30;
var startDate=new Date();
var endDate = new Date();
endDate.setDate(endDate.getDate() + days);
var strDate = startDate.getFullYear() + "-" + (startDate.getMonth()+1) + "-" + startDate.getDate();
$("#from_date").val(strDate);
$("#to_date").val(endDate.getFullYear()+'-'+(endDate.getMonth() + 1)+'-'+endDate.getDate());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="period" />
<input type="text" id="from_date" />
<input type="text" id="to_date" />
答案 1 :(得分:1)
您可以在这里使用moment.js
[user@salikhov ~/workspace/mqtt-rust $ RUST_BACKTRACE=1 cargo run
Compiling mqtt v0.1.0 (file:///home/user/workspace/mqtt-rust)
Running `target/debug/mqtt`
thread '<main>' panicked at 'I want line numbers!', src/proto/client.rs:33
stack backtrace:
1: 0x7ff049fa47d9 - sys::backtrace::tracing::imp::write::he18882fa84e6b00ePnt
2: 0x7ff049fa39b8 - panicking::on_panic::h495226a97f084523enx
3: 0x7ff049f9dcce - sys_common::unwind::begin_unwind_inner::h7a4ee06c0d57e26affs
4: 0x7ff049f95f47 - sys_common::unwind::begin_unwind::h13029855766851973181
at ../src/libstd/sys/common/unwind/mod.rs:232
5: 0x7ff049f95e8a - proto::client::MqttConnection::connect::h633d3d42c15a3dedgYa
at /home/user/workspace/mqtt-rust/<std macros>:3
6: 0x7ff049f80416 - main::h1d77c75265710f92gaa
at src/main.rs:5
7: 0x7ff049fa6084 - sys_common::unwind::try::try_fn::h4848098439110500489
8: 0x7ff049fa3098 - __rust_try
9: 0x7ff049fa5cf8 - rt::lang_start::hcf64c98c1a7c0031Zkx
10: 0x7ff049f834f6 - main
11: 0x7ff049170ec4 - __libc_start_main
12: 0x7ff049f802a8 - <unknown>
13: 0x0 - <unknown>
An unknown error occurred
var days = 10;
var dateFormat = 'YYYY-MM-DD';
var startFormatted = moment().format(dateFormat);
var endFormatted = moment().add(days, 'day').format(dateFormat);
console.log(startFormatted)
console.log(endFormatted)
答案 2 :(得分:0)
您没有正确添加日期。
function getDateonperiod(elem)
{
var periodval=$("#period"+elem).val();
var days = periodval * 30;
var startDate=new Date();
var endDate = new Date(startDate.getFullYear(),startDate.getMonth(),startDate.getDate()+n);
var strDate = startDate.getFullYear() + "-" + (startDate.getMonth()+1) + "-" + startDate.getDate();
$("#from_date"+elem).val(strDate);
$("#to_date"+elem).val((endDate.getFullYear()+'-'+endDate.getMonth() + 1)+'-'+endDate.getDate());
}
答案 3 :(得分:0)
只需使用此库 http://momentjs.com/
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js">
你的代码与momentjs
function getDateonperiod(elem)
{
var periodval=$("#period"+elem).val();
var days = periodval * 30;
var startDate = new moment();
var endDate = new moment();
endDate.add(days,'days');
var strStartDate = startDate.format("YYYY - MM - DD");
var strEndDate = endDate.format("YYYY - MM - DD");
$("#from_date"+elem).val(strDate);
$("#to_date"+elem).val(strEndDate);
}
答案 4 :(得分:0)
我只是扩展日期功能以添加天数。这可能会对您有所帮助:
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + parseInt(days));
return this;
};
$("#period").change(function() {
var periodval = $("#period").val();
var days = periodval;
var startDate = new Date();
var endDate = new Date();
endDate.addDays(days);
//endDate.setDate(endDate.getDate() + days);
var strDate = startDate.getFullYear() + "-" + (startDate.getMonth() + 1) + "-" + startDate.getDate();
$("#from_date").val(strDate);
$("#to_date").val(endDate.getFullYear() + '-' + (endDate.getMonth() + 1) + '-' + endDate.getDate());
});