Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert NgbDate to js Date object?

I am using an ng-bootstrap ngbDate object. I would like to convert it to a standard Date object. How do you do this?

Here is the relevant part of the .ts file:

import {NgbCalendar, NgbDate} from "@ng-bootstrap/ng-bootstrap";
fromDate: NgbDate;
toDate: NgbDate;
constructor(calendar: NgbCalendar) {
    this.fromDate = calendar.getToday();
    this.toDate = calendar.getNext(calendar.getToday(), 'd', 10);
    }
like image 344
Baruch Gans Avatar asked Apr 29 '19 12:04

Baruch Gans


People also ask

How do I change the date format in NgbDatepicker?

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.

What is NgbDateStruct?

NgbDateStruct Interface of the model of the NgbDatepicker and NgbInputDatepicker directives. Properties. day Type: number The day of month, starting at 1. month Type: number The month, with default calendar we use ISO 8601: 1=Jan ...


1 Answers

According to ngb-date.ts, NgbDate is just a container for year, month and day.

So, you could just do:

const jsDate = new Date(ngbDate.year, ngbDate.month - 1, ngbDate.day);
like image 78
erikvimz Avatar answered Oct 08 '22 15:10

erikvimz