如何解决当日期为今天时,BsDatePicker返回无效日期
我有这个角度分量
HTML:
<label class="col-form-label" for="fecha">{{ label }}</label>.
<span style="color: red" *ngIf="requerido && !validarFecha()"> {{espacio}} * </span>
<div class="input-group">
<input
placement="bottom left"
[bsConfig]="{ dateInputFormat: 'DD/MM/YYYY',containerClass: 'theme-dark-blue' }"
[(bsValue)]="model"
value="{{ model | date: 'dd/MM/yyyy'}}"
name="fecha"
autocomplete="off"
[readonly]="soloLectura"
[isDisabled]="soloLectura"
type="text"
(focusout)="onFocusOut()"
placeholder="Ingrese una fecha"
class="form-control"
[required]="requerido"
bsDatepicker
(bsValueChange)="cambioElementoSeleccionado()"
#dp="bsDatepicker">
<span (click)="dp.show()" class="input-group-append">
<label class="input-group-text"><i style="cursor:pointer" class="fa fa-calendar"></i></label>
</span>
</div>
<input #jaja="ngModel" name="fechaValue" [(ngModel)]="model" [required]="requerido" hidden>
<app-input-validation *ngIf="requerido && !validarFecha()" [input]="jaja"></app-input-validation>
Ts:
import { Component,OnInit,Output,EventEmitter,Input,ViewChild,HostListener,AfterContentChecked,AfterViewChecked } from '@angular/core';
import { BsDatepickerDirective,BsLocaleService,defineLocale,esDoLocale } from 'ngx-bootstrap';
import { DatePipe } from '@angular/common';
import { NgForm,NgControl } from '@angular/forms';
import * as moment from 'moment';
@Component({
selector: 'app-domisys-fecha',templateUrl: './domisys-fecha.component.html'
})
export class DomisysFechaComponent implements OnInit {
private valorSeleccionado = moment();
@ViewChild(BsDatepickerDirective,{static: false}) datepicker: BsDatepickerDirective;
@ViewChild('jaja',{static: false}) control: NgControl;
@Output() cambioValor = new EventEmitter();
// @Input() model: any = moment();
@Input()
get model(): any {
return this.valorSeleccionado; //this.valorSeleccionado;
}
set model(valor: any) {
this.valorSeleccionado = valor;
}
getConverted(value) {
console.log(moment(value).format("DD/MM/YYYY"));
return moment(value).format("YYYY/MM/DD");
}
@Input() requerido = false;
@Input() label: string;
@Input() soloLectura = false;
public espacio = ' ';
public entro = false;
@HostListener('window:scroll')
onScrollEvent() {
this.datepicker.hide();
}
constructor(private localeService: BsLocaleService,private dp: DatePipe) { }
public ngOnInit(): void {
defineLocale('es',esDoLocale);
this.localeService.use('es');
}
public cambioElementoSeleccionado(event: any) {
this.cambioValor.emit({ fecha: this.model,event: event });
}
public validarFecha(): boolean {
let stringFecha = '';
if (this.model !== undefined && this.model !== null) {
stringFecha = this.model.toString();
}
const [d,m,y] = stringFecha.split('/');
const dateConversion = new Date(`${y}/${m}/${d}`);
return !(dateConversion.toString() == "Invalid Date");
}
public onFocusOut() {
if (!this.validarFecha()) {
this.entro=true;
} else {
this.entro=false;
}
}
public markAsDirty(): void {
this.control.control.markAsDirty();
}
}
我遇到的问题是,从服务器返回日期时,我得到了“无效日期”,但仅对于具有今天日期的记录,如果我选择了过去的记录,则显示得很好。
此记录已于今天注册。
现在,我已经在日历中添加了一天,当我检查记录时,日期可以正确显示
我现在不知道为什么会这样,希望您能帮助我。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。