CocosCreator3.x开发笔记3:时间相关
2023-01-08 本文已影响0人
原味蛋炒饭
秒转换为时分秒
private formatTime(totalSeconds: number): string
{
let hours: number = this.rounding((totalSeconds / 3600));
let hh: string = (hours < 10 ? "0" + hours : hours).toString();
let minutes: number = this.rounding((totalSeconds - hours * 3600) / 60);
let mm: string = minutes < 10 ? "0" + minutes : minutes.toString();
let seconds: number = this.rounding(totalSeconds - hours * 3600 - minutes * 60);
let ss: string = seconds < 10 ? "0" + seconds : seconds.toString();
let num: string = hh + ":" + mm + ":" + ss;
return num;
}
private rounding(num: number): number
{
return Math.floor(num);
}
获取时间戳
/*** 时间戳换算日期* */
formatDateTimeBySelf(): string {
var date = new Date();
// date.setTime(timeStamp * 1000);
let y = date.getFullYear();
let m = date.getMonth() + 1;//月份是0-11
let mS = m < 10 ? ('0' + m.toString()) : m.toString();
let d = date.getDate();
let dS = d < 10 ? ('0' + d.toString()) : d.toString();
let h = date.getHours();
let hS = h < 10 ? ('0' + h.toString()) : h.toString();
var minute = date.getMinutes();
var second = date.getSeconds();
let minuteS = minute < 10 ? ('0' + minute) : minute;
let secondS = second < 10 ? ('0' + second) : second;
return y + '-' + mS + '-' + dS + ' ' + hS + ':' + minuteS + ':' + secondS;
}
number转年月日
formatDateTimeBySelf(saveTimeN: number): string {
let date = new Date(saveTimeN)
// date.setTime(timeStamp * 1000);
let y = date.getFullYear();
let m = date.getMonth() + 1;//月份是0-11
let mS = m < 10 ? ('0' + m.toString()) : m.toString();
let d = date.getDate();
let dS = d < 10 ? ('0' + d.toString()) : d.toString();
let h = date.getHours();
let hS = h < 10 ? ('0' + h.toString()) : h.toString();
var minute = date.getMinutes();
var second = date.getSeconds();
let minuteS = minute < 10 ? ('0' + minute) : minute;
let secondS = second < 10 ? ('0' + second) : second;
return y + '-' + mS + '-' + dS + ' ' + hS + ':' + minuteS + ':' + secondS;
}