问题描述
ionic cordova app 开发,ion-datetime 设置当前时间为默认时间,日期格式为 yyyy-MM-dd HH:mm:ss
Android 设置默认时间的值可以正常显示,在iphone上设置的默认时间没有显示
原因及解决方法
后来,仔细看了ion-datetime 日期格式化的相关说明,发现如果是单独使用日期,比如 yyyy-MM-dd 或者HH:mm:ss的日期格式,后台直接获取相对应的格式的日期,显示没有问题,比如“2020-07-29”或者 “12:30:45”,但是如果使用日期+时间组合的方式设置默认时间的话,需要将时间格式转换为 “yyyy-MM-ddTHH:mm:ss+08:00”(ISO 8601 Datetime Format: YYYY-MM-DDTHH:mmZ ),与时区的设置有关系
附问题解决前后的相关代码
问题解决前的前端代码
<ion-datetime displayFormat="YYYY-MM-DD HH:mm:ss" value="sdate" [(ngModel)]="sdate"
placeholder="{{i18nMsg.startdate}}">
</ion-datetime>
问题解决前的后端代码
sdate:string='';
ngOnInit() {
const currdate = this.getCurrentDate();
this.sdate = currdate;
}
/*获取当前时间 */
getCurrentDate() {
const date = new Date();
const year: number = date.getFullYear();
const month: any = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
const day: any = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
const hour: any = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
const minute: any = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
const seconds: any = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return year + '-' + month + '-' + day + " " + hour + ":" + minute + ":" + seconds;
}
问题解决后的后台代码
sdate:string='';
ngOnInit() {
const currdate = this.getCurrentDate();
this.sdate = currdate;
}
/*获取当前时间 */
getCurrentDate() {
const date = new Date();
return this.dateFormat(date, "yyyy-MM-ddTHH:mm:ss+08:00");
}
/**
* 日期对象转为日期字符串
* @param date 需要格式化的日期对象
* @param sFormat 输出格式,默认为yyyy-MM-dd 年:y,月:M,日:d,时:h,分:m,秒:s
* @example dateFormat(new Date()) "2017-02-28"
* @example dateFormat(new Date(),'yyyy-MM-dd') "2017-02-28"
* @example dateFormat(new Date(),'yyyy-MM-dd HH:mm:ss') "2017-02-28 13:24:00" ps:HH:24小时制
* @example dateFormat(new Date(),'yyyy-MM-dd hh:mm:ss') "2017-02-28 01:24:00" ps:hh:12小时制
* @example dateFormat(new Date(),'hh:mm') "09:24"
* @example dateFormat(new Date(),'yyyy-MM-ddTHH:mm:ss+08:00') "2017-02-28T13:24:00+08:00"
* @example dateFormat(new Date('2017-02-28 13:24:00'),'yyyy-MM-ddTHH:mm:ss+08:00') "2017-02-28T13:24:00+08:00"
* @returns {string}
*/
dateFormat(date: Date, sFormat: String = 'yyyy-MM-dd'): string {
let time = {
Year: 0,
TYear: '0',
Month: 0,
TMonth: '0',
Day: 0,
TDay: '0',
Hour: 0,
THour: '0',
hour: 0,
Thour: '0',
Minute: 0,
TMinute: '0',
Second: 0,
TSecond: '0',
Millisecond: 0
};
time.Year = date.getFullYear();
time.TYear = String(time.Year).substr(2);
time.Month = date.getMonth() + 1;
time.TMonth = time.Month < 10 ? "0" + time.Month : String(time.Month);
time.Day = date.getDate();
time.TDay = time.Day < 10 ? "0" + time.Day : String(time.Day);
time.Hour = date.getHours();
time.THour = time.Hour < 10 ? "0" + time.Hour : String(time.Hour);
time.hour = time.Hour < 13 ? time.Hour : time.Hour - 12;
time.Thour = time.hour < 10 ? "0" + time.hour : String(time.hour);
time.Minute = date.getMinutes();
time.TMinute = time.Minute < 10 ? "0" + time.Minute : String(time.Minute);
time.Second = date.getSeconds();
time.TSecond = time.Second < 10 ? "0" + time.Second : String(time.Second);
time.Millisecond = date.getMilliseconds();
return sFormat.replace(/yyyy/ig, String(time.Year))
.replace(/yyy/ig, String(time.Year))
.replace(/yy/ig, time.TYear)
.replace(/y/ig, time.TYear)
.replace(/MM/g, time.TMonth)
.replace(/M/g, String(time.Month))
.replace(/dd/ig, time.TDay)
.replace(/d/ig, String(time.Day))
.replace(/HH/g, time.THour)
.replace(/H/g, String(time.Hour))
.replace(/hh/g, time.Thour)
.replace(/h/g, String(time.hour))
.replace(/mm/g, time.TMinute)
.replace(/m/g, String(time.Minute))
.replace(/ss/ig, time.TSecond)
.replace(/s/ig, String(time.Second))
.replace(/fff/ig, String(time.Millisecond))
}