Posts

Showing posts with the label date

Formatting Date in Angular without the time

Formatting Date in Angular without the time In my component I pass the following values to the controls. The value I pass are both of type date. this.form.controls["DepartureDate"].setValue(this.flightChoice.departureDate); this.form.controls["ReturnDate"].setValue(this.flightChoice.returnDate); The controls are of type Text because if I use type date the value are not written at all. <input class="form-control" formControlName="DepartureDate" type="text" id="departureDate" /> <input class="form-control" formControlName="ReturnDate" type="text" id="returnDate" /> Now the result of passing the values is: a date such this: 2018-06-30T00:00:00+02:00 when I would like a date such: 30-06-2018 What can I do to have this kind of value and also why using input of type "date" will not accept my values? You can use any pipe also in Typescript by pr...

How can I find closest date value from an array according to the current date?

How can I find closest date value from an array according to the current date? I want to get the next closest date on this array according to the current date. var dates = [ 'Aug 18, 2018 03:24:00', 'August 19, 2018 03:24:00', 'September 17, 2018 03:24:00', 'September 14, 2018 03:24:00', 'August 18, 2018 03:24:00', 'July 16, 2018 03:24:00', 'July 15, 2018 03:24:00', 'December 15, 2018 03:24:00', 'July 13, 2018 03:24:00', ]; var now = new Date(); 4 Answers 4 First you need to convert each date into a timestamp then subtract each of them by the current date and store the timestamp difference in the temp array then get the index of the minimum value and use the index to access the closest date in the original array. var dates = [ 'July 16, 1995 03:24:00', 'Aug 18, 1995 03:...