[英]How to change the date format in Date Picker
I am facing one issue fomrating the date我面临一个日期问题
Here is my function这是我的 function
$('input[name="issued_date"]').daterangepicker({
singleDatePicker: true,
showDropdowns: true,
issued_date: moment().startOf('hour'),
locale: {
"format": "YYYY-MM-DD",
"separator": "-"
}
});
$('input[name="expiry_date"]').daterangepicker({
singleDatePicker: true,
showDropdowns: true,
expiry_date: moment().startOf('hour'),
locale: {
"format": "YYYY-MM-DD",
"separator": "-"
}
});
It showing dates and time in format like this "2019-05-30"它以“2019-05-30”这样的格式显示日期和时间
If I change it like this如果我像这样改变它
$('input[name="issued_date"]').daterangepicker({
singleDatePicker: true,
showDropdowns: true,
issued_date: moment().startOf('hour'),
locale: {
"format": "DD-MM-YYYY",
"separator": "-"
}
});
$('input[name="expiry_date"]').daterangepicker({
singleDatePicker: true,
showDropdowns: true,
expiry_date: moment().startOf('hour'),
locale: {
"format": "DD-MM-YYYY",
"separator": "-"
}
});
The format it shows correct.... It will show 05-05-2020 But I am facing one issue in another function.... I need to check the start_date should less than end_date...它显示的格式正确....它将显示 05-05-2020 但我在另一个 function 中面临一个问题....我需要检查 start_date 应该小于 end_date...
So at the time of form submission.... I am returning another function like this所以在提交表单时....我正在返回另一个 function 像这样
$(".someclass").click(function () {
$('.loading').show();
var start_date = document.getElementById('id_issued_date').value;
var end_date = document.getElementById('id_expiry_date').value;
if (start_date < end_date)
{
var url = "{% url 'add' %}";
$.ajax({
type: 'POST',
url: url,
data: $("#new_form").serialize(),
cache: false,
success: function (data, status) {
if (data['status'] == "success") {
$('#form_modal').modal('hide');
toastr.success(data.message);
$('#form_modal').children().remove();
url = getPathFromUrl(document.location.href)
window.location.href = url
}
else {
$('#form_modal').html(data);
}
}
});
}
else{
toastr.error('start date should be less than End Date');
}
});
But if I Pass a date like this start date 01/06/2019 and end date is 05/05/2020 it will not work但是,如果我通过一个像这样的开始日期 01/06/2019 并且结束日期是 05/05/2020 的日期,它将不起作用
Actually the start_Date is less than end date实际上 start_Date 小于结束日期
create objects of Date
and compare.创建
Date
对象并进行比较。
JavaScript will use the browser's time zone and display a date as a full text string. JavaScript 将使用浏览器的时区并将日期显示为全文字符串。 eg:
例如:
let now = new Date()
// now will be equal to Thu May 07 2020 15:08:04 GMT+0530 (India Standard Time)
So, in this case:所以,在这种情况下:
var start_date = document.getElementById('id_issued_date').value;
var end_date = document.getElementById('id_expiry_date').value;
let start_date_obj = new Date(start_date);
let end_date_obj = new Date(end_date);
if ( start_date_obj < end_date_obj )
{
// do something
}
you don't have to worry the date format.您不必担心日期格式。 ie, whether it is in YYYY-MM-DD or DD-MM-YYYY formats or if it has a different seperator.
即,它是 YYYY-MM-DD 还是 DD-MM-YYYY 格式,或者它是否具有不同的分隔符。 (but should be a date)
(但应该是日期)
Instead of comparing the string representation of the dates using <
.而不是使用
<
比较日期的字符串表示形式。 Use Moment.js to compare the two values of the date pickers.使用 Moment.js 比较日期选择器的两个值。
The Date Range Picker documentation says the following:日期范围选择器文档说明如下:
You can access the Date Range Picker object and its functions and properties through data properties of the element you attached it to.
您可以通过附加到的元素的数据属性访问日期范围选择器 object 及其功能和属性。
var drp = $('#daterange').data('daterangepicker');
Then use Momenent.js isBefore()
to compare the two values:然后使用 Momenent.js
isBefore()
比较两个值:
date1.isBefore(date2)
const $issued_date = $('input[name="issued_date"]'), $expiry_date = $('input[name="expiry_date"]'), $check_dates = $('button[name="check_dates"]'), defaultOptions = { singleDatePicker: true, showDropdowns: true, locale: { format: "DD-MM-YYYY", separator: "-" }, }; $issued_date.daterangepicker(Object.assign({}, defaultOptions, { issued_date: moment().startOf('hour'), })); $expiry_date.daterangepicker(Object.assign({}, defaultOptions, { expiry_date: moment().startOf('hour'), })); $check_dates.on("click", () => { const issuedDate = $issued_date.data("daterangepicker").startDate, expiryDate = $expiry_date.data("daterangepicker").startDate; console.log(issuedDate.isBefore(expiryDate)); });
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script> <script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" /> <input type="text" name="issued_date" /> <input type="text" name="expiry_date" /> <button type="button" name="check_dates">issued_date < expiry_date</button>
Dear亲爱的
You can try to use this.你可以尝试使用这个。 This is very powerful
这是非常强大的
today = moment(new Date()).format('YYYY-MM-DD')
You can use any kinds of separator like this您可以像这样使用任何类型的分隔符
"('YYYY-MM-DD')"
"('YYYY/MM/DD')"
"('YYYY.MM.DD')"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.