我有一个基于分钟和小时形成时间的方法。 所以我在这里想要的是添加 AM PM
customTime(time_type, formula) {
this.isCustomTime = true;
let hour;
let minute;
if(time_type == 'hour' && formula == 'add'){
hour = Number(this.defaultHour) + 1;
if(hour > 12) {
hour = 1;
}
this.defaultHour = ('0' + hour).slice(-2);
}else if(time_type == 'hour' && formula == 'minus'){
hour = Number(this.defaultHour) - 1;
if(hour < 1){
hour = 12;
}
this.defaultHour = ('0' + hour).slice(-2);
}else if(time_type == 'minute' && formula == 'add'){
minute = Number(this.defaultMinute);
hour = Number(this.defaultHour);
if(minute < 30) {
minute = 30;
}else{
minute += 30;
if(minute > 59) {
minute = 0;
hour += 1;
if(hour > 12) {
hour = 1;
}
this.defaultHour = ('0' + hour).slice(-2);
}
}
this.defaultMinute = ('0' + minute).slice(-2);
}else if(time_type == 'minute' && formula == 'minus'){
minute = Number(this.defaultMinute);
hour = Number(this.defaultHour);
if(minute > 30 || minute == 0) {
minute = 30;
hour -= 1;
if(hour < 1){
hour = 12;
}
this.defaultHour = ('0' + hour).slice(-2);
}else {
minute = 0;
}
this.defaultMinute = ('0' + minute).slice(-2);
}
}
注意:它正在工作。这里只缺少时间的AM/PM
。那么我可以将它们添加到我的代码中,或者您是否有一些简单的方法来制作我想要的东西?
这是计算的前端
<ion-header>
<ion-navbar>
<ion-title>
Home
</ion-title>
<ion-buttons end>
<button ion-button icon-only (click)="refresh()">
<ion-icon name="refresh"></ion-icon>
</button>
</ion-buttons>
<ion-buttons end>
<button ion-button icon-only (click)="showTimeZoneModal()">
<ion-icon name="add"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content>
<ion-card>
<ion-card-header>
{{ selectedTimeZone }}
</ion-card-header>
<ion-card-content>
<div class="time">
{{ defaultHour }} : {{ defaultMinute }} : {{ defaultSecond }} : {{ defaultMidday }}
</div>
<div class="date">
{{ defaultDate }}
<br>
{{ defaultDay }}
</div>
</ion-card-content>
</ion-card>
<ion-card>
<ion-card-content>
<h2>Hour</h2>
<button ion-item (click)="customTime('hour', 'add')"><ion-label>+</ion-label></button>
<button ion-item (click)="customTime('hour', 'minus')"><ion-label>-</ion-label></button>
<h2>Minute</h2>
<button ion-item (click)="customTime('minute', 'add')"><ion-label>+</ion-label></button>
<button ion-item (click)="customTime('minute', 'minus')"><ion-label>-</ion-label></button>
</ion-card-content>
</ion-card>
<ion-list>
<ion-item *ngFor="let zone of timezone; let i = index" (click)="selectTimeZone(zone, i)">
{{ zone.name }}
</ion-item>
</ion-list>
</ion-content>
如果您想以AM/PM格式显示时间,那么简单的方法就是从互联网下载/添加 D3 自由。
然后使用
var timeOfDay=d3.timeFormat("%H:%M:%S %p")(new Date());
console.log(timeOfDay);//your desired output
如果您只有小时和分钟,则用作波纹管
var hour=12;
var mminutes=55;
var timeOfDay=new Date();
timeOfDay.setHours(hour);
timeOfDay.setMinutes(mminutes);
d3.timeFormat("%H:%M %p")(timeOfDay)
console.log(d3.timeFormat("%H:%M %p")(timeOfDay)
);