[英]Is there a way to have good date format with jquery datetime picker?
I am trying to develop a screen with Angular 9.1.3 and bootstrap and I would like to use jquery datetime-picker.我正在尝试使用 Angular 9.1.3 和引导程序开发一个屏幕,我想使用 jquery 日期时间选择器。 I have integrated in the screen and it select the date but it return the date in the field, the format is wrong.我已经在屏幕中集成了 select 日期,但它在字段中返回日期,格式错误。 I have look at many examples but I did not succeed to solve my problem.我看过很多例子,但我没有成功解决我的问题。
In html file I have declare the field as follows:在 html 文件中,我声明了如下字段:
<div class="form-group row">
<label for="startDate" class="col-sm-2 col-form-label" i18n="@@startDate">Start date</label>
<div id="startDatePicker" class="col-sm-3 input-append date">
<input data-format="dd/MM/yyyy hh:mm" type="text" class="form-control onlyDateTime" formControlName="startDate"
(click)="selectDate($event)">
</div>
<span class="input-group-addon">
<span class="fa fa-calendar"></span>
</span>
</div>
In the typescript file, I have create the selectDate method as follows:在 typescript 文件中,我创建了 selectDate 方法,如下所示:
selectDate(event: Event) {
const pickerOpts = {
dateFormat: 'yyyy/MM/dd hh:mm',
};
$('.onlyDateTime').datetimepicker(pickerOpts);
}
but the picker return english date format: yyyy/MM/dd hh:mm但选择器返回英文日期格式:yyyy/MM/dd hh:mm
I have tried to locale: 'fr-FR' in datetime-picker options but I have always the same problem.我尝试在日期时间选择器选项中设置语言环境:'fr-FR',但我总是遇到同样的问题。
the documentation of datetimepicker states that it uses the php-date-formatter by default to format dates. datetimepicker的文档声明它默认使用 php-date-formatter 来格式化日期。 So what you're looking for is:所以你要找的是:
format:'d/m/Y H:i'
in your picketOpts
variable to format the date as you want.在您的picketOpts
变量中根据需要格式化日期。
Here is list of different format options you can play around for datetimepicker
.这是您可以为datetimepicker
使用的不同格式选项的列表。
I have tried your proposal but it is always the same problem.我试过你的建议,但总是同样的问题。 I have modify my function like below:我已经修改了我的 function,如下所示:
selectDate(event: Event) {
var dateTmp: Date;
const pickerOpts = {
dateFormat: 'd/m/Y H:i',
};
$('.onlyDateTime').datetimepicker(pickerOpts);
$('.onlyDateTime').change((selectedDate) => {
if (selectedDate !== undefined) {
dateTmp = $(this)[0].editedItem.startDate;
console.log('Date inside: ' + dateTmp);
}
});
console.log('Date Outside: ' + dateTmp);
}
The date inside jquery function is fine. jquery function 里面的日期没问题。 The date outside is always undefined.外面的日期总是不确定的。 Is it a way to get the value of a variable inside a jquery function from outside.这是一种从外部获取 jquery function 内部变量值的方法吗? With this value I am able to format the date using DatePipe transformer furnished by @angular/common.有了这个值,我可以使用@angular/common 提供的 DatePipe 转换器来格式化日期。
If you are using in browser, you can use Date.toLocaleDateString如果您在浏览器中使用,则可以使用Date.toLocaleDateString
For better support, use momentjs: https://momentjs.com/为了获得更好的支持,请使用 momentjs: https://momentjs.com/
const format = (d) => { return d.toLocaleDateString("fr-FR", { year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric", }); }; console.log(format(new Date()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.