I'm working with ngbDatepicker this is giving me JSON date format like:
{ year: 2019, month: 6, day: 9 }
How can I convert this into YYYY/MM/DD? I'm using Angular 7.
My Code is as below:
<div class="input-group">
<input class="form-control" placeholder="YYYY-MM-DD"
(click)="d2.toggle()" (ngModelChange)="onDateSelection($event,'ToDate');" name="d2" #c2="ngModel" [(ngModel)]="toDate" ngbDatepicker #d2="ngbDatepicker">
<div class="input-group-append">
<button class="btn btn-outline-secondar"
(click)="d2.toggle()" type="button">
<i class="fa fa-calendar" aria-hidden="true"></i>
</button>
</div>
</div>
There two important class to manage ngbDate. one it's for formatting the date: a DateParserFormater, and another to change the value you get/send from/to a ngb-datepicker: a DateAdapter.
So, you can create a customDateAdapter and a customDateParserFormatter. But, don't worry about the names. ist only two injectable class like, e.g.
For customDateAdapter
@Injectable()
export class CustomDateAdapter {
fromModel(value: string): NgbDateStruct
{
if (!value)
return null
let parts=value.split('/');
return {year:+parts[0],month:+parts[1],day:+parts[2]}
}
toModel(date: NgbDateStruct): string // from internal model -> your mode
{
return date?date.year+"/"+('0'+date.month).slice(-2)
+"/"+('0'+date.day).slice(-2):null
}
}
Yes a injectable class with two functions, one to transform a NgbDate to string and another to transform string to NgbDate. remember that this change your "model"
For CustomDateParserFormatter
@Injectable()
export class CustomDateParserFormatter {
parse(value: string): NgbDateStruct
{
if (!value)
return null
let parts=value.split('/');
return {year:+parts[0],month:+parts[1],day:+parts[2]} as NgbDateStruct
}
format(date: NgbDateStruct): string
{
return date?date.year+"/"+('0'+date.month).slice(-2)+"/"+('0'+date.day).slice(-2):null
}
}
Again an injectable class with two functions, one to transform a NgbDate to string and another to transform string to NgbDate. Remember that this change the "format" of the date -useful if you want, e.g. dd/MM/yyyy-
Then just add as providers in your component
providers: [{provide: NgbDateAdapter, useClass: CustomDateAdapter},
{provide: NgbDateParserFormatter, useClass: CustomDateParserFormatter}]
In the stackblitz see the definition of component, you can choose, e.g.
@Component({
selector: 'ngbd-datepicker-adapter',
templateUrl: './datepicker-adapter.html',
providers: [{provide: NgbDateAdapter, useClass: NgbDateNativeAdapter}]
})
or
@Component({
selector: 'ngbd-datepicker-adapter',
templateUrl: './datepicker-adapter.html',
providers: [{provide: NgbDateAdapter, useClass: CustomDateAdapter},
{provide: NgbDateParserFormatter, useClass: CustomDateParserFormatter}]
})
even you can write
@Component({
selector: 'ngbd-datepicker-adapter',
templateUrl: './datepicker-adapter.html',
providers: [{provide: NgbDateParserFormatter, useClass: CustomDateParserFormatter}]
})
To manteinance the objects like {year,month,day}, but change the "mask" -and the way you input the date
NOTE: You can add the providers to the module too
What you need is
var date = object.year + '/' + object.month + '/'+ object.day
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With