Java Date getHours()并在Angular页面中显示

问题描述 投票:0回答:1

当我点击Create new Ticket#1时,我能够自动显示当前年/月/日,如下所示:enter image description here但我还需要时间。

以下是我实现它的方法:

我的Java Pojo:

public class Ticket implements Serializable {

@Column(name = "jhi_date")
    private LocalDate date;

//getters and setters
}

我的票 - popup.service.ts

setTimeout(() => {

                  // populate date with current date if new
                    const tickets = new Ticket();
                    const now = new Date();
                    tickets.date = {
                        year: now.getFullYear(), // works fine
                        month: now.getMonth() + 1, // works fine
                        day: now.getDate(), // works fine
                       time: now.getTime(), // doesnt return anything as shown in image
                       hour: now.getHours() // doesnt return anything as in image
                    };

                    this.ngbModalRef = this.ticketModalRef(component, tickets);
                    resolve(this.ngbModalRef);
                }, 0);

这很可能是由ngbDatepicker组件引起的。什么可能相当于替换?

 <div class="form-group">
            <label class="form-control-label" for="field_date">Date</label>
            <div class="input-group">
                <input id="field_date" type="text" class="form-control" name="date" ngbDatepicker  #dateDp="ngbDatepicker" [(ngModel)]="ticket.date"
                />
                <span class="input-group-append">
                    <button type="button" class="btn btn-secondary" (click)="dateDp.toggle()"><i class="fa fa-calendar"></i></button>
                </span>
            </div>
        </div>

Github样本here

javascript angular typescript spring-boot jhipster
1个回答
0
投票

以下是我设法获得时间的方法:

public class Ticket implements Serializable {

 //@Column(name = "jhi_date") 
 //private LocalDate date;
 @Column(name = "jhi_timestamp")
 private ZonedDateTime timestamp; // used ZonedDateTime instead of LocalDate 

//getters and setters
}

在我的ticket-popup.service.ts中使用Angular Date Pipe

setTimeout(() => {

                  // populate date/time with current time if new
                    const ticket = new Ticket();
                    ticket.timestamp = this.datePipe // used Pipe date format 
                        .transform(new Date(), 'yyyy-MM-ddThh:mm');


                    this.ngbModalRef = this.ticketModalRef(component,  ticket);
                    resolve(this.ngbModalRef);
                }, 0);

从ticket-dialog.component.html中删除了ngbDatepicker

<div class="form-group">
            <label class="form-control-label" for="field_timestamp">Timestamp</label>
            <div class="d-flex">
                <input id="field_timestamp" type="datetime-local" class="form-control" name="timestamp" [(ngModel)]="ticket.timestamp"
                />
            </div>
        </div>

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.