Daterangepicker moment() returns 个数字
Daterangepicker moment() returns numbers
我有一个 .cshtml,其中实施了 daterangepicker.js。该页面通过 ViewBag 从父页面获取日期范围(到日期,从日期)。代码如下:
var _FromDate;
var _EndDate;
$(function () {
var from1 = '@ViewBag.from_date';
var from = from1.split("-");
var f = new Date(from[0], from[1] - 1, from[2]);
var to1 = '@ViewBag.to_date';
var to = to1.split("-");
var t = new Date(to[0], to[1] - 1, to[2]);
var start = (f.getFullYear() + '-' + pad((f.getMonth() + 1), 2) + '-' + pad(f.getDate(), 2));
var end = (t.getFullYear() + '-' + pad((t.getMonth() + 1), 2) + '-' + pad(t.getDate(), 2));
function cb(start, end) {
$('#reportrange span').html(start + ' . ' + end);
var dateRange = $('#reportrange span').html();
_FromDate = dateRange.substring(0, dateRange.indexOf('.'));
_EndDate = dateRange.substring(dateRange.indexOf('.') + 1);
};
function pad(str, max) {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
$('#reportrange').daterangepicker({
startDate: start,
endDate: end,
locale: { format: "YYYY-MM-DD" },
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
}
}, cb);
cb(start, end);
});
现在的问题是:当 运行 页面时,daterangepicker returns 日期为
a string of numbers
问题的原因是什么?我一步一步的把代码过一遍,也没找到问题所在。
感谢任何帮助!
你有一些不必要的代码,我做了一个重写 plunk https://embed.plnkr.co/G3xxp0HWPOoFYuF8u2qd/
/* global variables to store your dates ..*/
var _FromDate;
var _EndDate;
$(function () {
var _format = "YYYY-MM-DD";
var start = moment('@ViewBag.from_date', _format);
var end = moment('@ViewBag.from_date', _format);
/* update your display summary and your local variables... */
function cb(start, end) {
/*
storing the moment object. If case you post these values over to a server, defaulr ajax transforms into ISOstring.
But in case it doesn't, your should _FromDate = start.toISOString() or if u have a sppecific format start.format('{your format}')
*/
_FromDate = start;
_EndDate = end;
$('#reportrange span').html(start.format(_format) + ' - ' + end.format(_format));
}
$('#reportrange').daterangepicker({
startDate: start,
endDate: end,
locale: { format: _format },
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
}
}, cb);
cb(start, end);
});
我有一个 .cshtml,其中实施了 daterangepicker.js。该页面通过 ViewBag 从父页面获取日期范围(到日期,从日期)。代码如下:
var _FromDate;
var _EndDate;
$(function () {
var from1 = '@ViewBag.from_date';
var from = from1.split("-");
var f = new Date(from[0], from[1] - 1, from[2]);
var to1 = '@ViewBag.to_date';
var to = to1.split("-");
var t = new Date(to[0], to[1] - 1, to[2]);
var start = (f.getFullYear() + '-' + pad((f.getMonth() + 1), 2) + '-' + pad(f.getDate(), 2));
var end = (t.getFullYear() + '-' + pad((t.getMonth() + 1), 2) + '-' + pad(t.getDate(), 2));
function cb(start, end) {
$('#reportrange span').html(start + ' . ' + end);
var dateRange = $('#reportrange span').html();
_FromDate = dateRange.substring(0, dateRange.indexOf('.'));
_EndDate = dateRange.substring(dateRange.indexOf('.') + 1);
};
function pad(str, max) {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
$('#reportrange').daterangepicker({
startDate: start,
endDate: end,
locale: { format: "YYYY-MM-DD" },
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
}
}, cb);
cb(start, end);
});
现在的问题是:当 运行 页面时,daterangepicker returns 日期为 a string of numbers
问题的原因是什么?我一步一步的把代码过一遍,也没找到问题所在。
感谢任何帮助!
你有一些不必要的代码,我做了一个重写 plunk https://embed.plnkr.co/G3xxp0HWPOoFYuF8u2qd/
/* global variables to store your dates ..*/
var _FromDate;
var _EndDate;
$(function () {
var _format = "YYYY-MM-DD";
var start = moment('@ViewBag.from_date', _format);
var end = moment('@ViewBag.from_date', _format);
/* update your display summary and your local variables... */
function cb(start, end) {
/*
storing the moment object. If case you post these values over to a server, defaulr ajax transforms into ISOstring.
But in case it doesn't, your should _FromDate = start.toISOString() or if u have a sppecific format start.format('{your format}')
*/
_FromDate = start;
_EndDate = end;
$('#reportrange span').html(start.format(_format) + ' - ' + end.format(_format));
}
$('#reportrange').daterangepicker({
startDate: start,
endDate: end,
locale: { format: _format },
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
}
}, cb);
cb(start, end);
});