convert to date string for inputs

This commit is contained in:
Tykayn 2021-11-17 16:52:16 +01:00 committed by tykayn
parent 706fd28698
commit 0f4aac7e81
7 changed files with 24 additions and 34 deletions

View File

@ -2,7 +2,7 @@ export interface DateChoice {
literal: string;
timeSlices: TimeSlices[];
date_object: Date;
date_input: String;
date_input: string;
}
export interface TimeSlices {

View File

@ -109,9 +109,9 @@ export class DateUtilitiesService {
const ladate4 = this.addDaysToDate(3, today);
return [
this.convertToDateChoiceObject(ladate2),
this.convertToDateChoiceObject(ladate3),
this.convertToDateChoiceObject(ladate4),
this.convertDateToDateChoiceObject(ladate2),
this.convertDateToDateChoiceObject(ladate3),
this.convertDateToDateChoiceObject(ladate4),
];
}
@ -119,12 +119,16 @@ export class DateUtilitiesService {
* convert a date to a DateChoice
* @param date
*/
convertToDateChoiceObject(date: Date): DateChoice {
convertDateToDateChoiceObject(date: Date): DateChoice {
let isUnder10 = date.getDate() < 10;
let day = isUnder10 ? `0${date.getDate()}` : date.getDate();
// get month is based on 0, so yeah
let input = `${date.getFullYear()}-${date.getMonth() + 1}-${day}`;
return {
literal: this.formateDateToInputStringNg(date),
timeSlices: Object.create(defaultTimeOfDay),
date_object: date,
date_input: `${date.getFullYear()}-${date.getMonth()}-${date.getDay()}`,
date_input: input,
};
}
}

View File

@ -667,7 +667,7 @@ export class PollService implements Resolve<Poll> {
convertCalendarToText() {
let converted = [];
for (let someDate of this.calendar) {
converted.push(this.DateUtilitiesService.convertToDateChoiceObject(someDate));
converted.push(this.DateUtilitiesService.convertDateToDateChoiceObject(someDate));
}
this.dateChoiceList = converted;
@ -678,11 +678,18 @@ export class PollService implements Resolve<Poll> {
* convert the DateChoices to an arrray of Dates for calendar picker
*/
convertTextToCalendar() {
console.log('convert text to calendar', this.dateChoiceList);
let converted = [];
for (let someDateChoice of this.dateChoiceList) {
converted.push(someDateChoice.date_object);
let dateObj = new Date(someDateChoice.date_input);
console.log('dateObj', dateObj);
// check that date is not part of the disabled dates
if (this.disabled_dates.indexOf(dateObj) === -1) {
converted.push(dateObj);
}
}
console.log('converted', converted);
this.calendar = converted;
return;
}
@ -741,7 +748,7 @@ export class PollService implements Resolve<Poll> {
for (let elem of this.calendar) {
console.log('elem', elem);
let converted_day = this.DateUtilitiesService.convertToDateChoiceObject(elem);
let converted_day = this.DateUtilitiesService.convertDateToDateChoiceObject(elem);
newpoll.dateChoices.push(converted_day);
}
console.log('newpoll.dateChoices', newpoll.dateChoices);

View File

@ -98,11 +98,7 @@
<app-shortcuts-help *ngIf="display"></app-shortcuts-help>
<br />
<br />
<app-day-list
[form]="form"
[dateChoices]="dateChoices"
[hasSeveralHours]="form.value.hasSeveralHours"
></app-day-list>
<app-day-list [form]="form" [hasSeveralHours]="form.value.hasSeveralHours"></app-day-list>
</div>
</div>
</div>

View File

@ -27,18 +27,11 @@
{{ choice.date_object | date: 'E':'Europe/Paris':'fr_FR' }}
</span>
</span>
{{ choice.date_input | date }}
<pre>
{{ choice.date_object | json }}
</pre
>
<input
[(ngModel)]="choice.date_input"
class="date-choice-item"
name="dateChoices_{{ id }}"
id="dateChoices_{{ id }}"
useValueAsDate
type="date"
/>
<button (click)="dateChoices.splice(id, 1)" class="btn btn-warning">

View File

@ -18,8 +18,7 @@ import { DateUtilitiesService } from '../../../../../../core/services/date.utili
export class DayListComponent {
@Input()
form: FormGroup;
@Input()
public dateChoices: Array<DateChoice> = [];
public dateChoices: DateChoice[] = [];
@Input()
public hasSeveralHours: boolean;
timeList: any;
@ -34,7 +33,7 @@ export class DayListComponent {
@Inject(DOCUMENT) private document: any,
private storageService: StorageService
) {
// this.setDemoTextChoices();
this.dateChoices = this.pollService.dateChoiceList;
}
reinitChoices(): void {
@ -130,11 +129,7 @@ export class DayListComponent {
}
addChoice(optionalLabel = ''): void {
this.pollService.dateChoiceList.push({
literal: '',
timeSlices: [],
date_object: new Date(),
});
this.pollService.dateChoiceList.push(this.dateUtilitiesService.convertDateToDateChoiceObject(new Date()));
this.focusOnChoice(this.storageService.dateChoices.length - 1);
}
@ -153,10 +148,6 @@ export class DayListComponent {
}
}
openKeyboardShortcutsModal() {
this.display = true;
}
isWeekendDay(date_object: Date) {
if (date_object) {
const day = date_object.getDay();

View File

@ -19,7 +19,6 @@
<div class="text-date-list" *ngIf="!mode_calendar">
<app-day-list
[form]="pollService.form"
[dateChoices]="pollService.dateChoiceList"
[hasSeveralHours]="pollService.form.value.hasSeveralHours"
></app-day-list>
</div>