javascript 时间转换整理

mac2024-04-21  5

/** * 生成具体日期时间 * @param timestamp 时间戳 * @type yyyy-mm-dd hh:mm:ss */ createTime(timestamp){ var date = timestamp ? new Date(timestamp) : new Date();//实例一个时间对象,timestamp无则是当前时间; var year = date.getFullYear();//获取系统的年; var month = date.getMonth() + 1;//获取系统月份,由于月份是从0开始计算,所以要加1 var day = date.getDate(); //获取系统日 var hour = date.getHours();//获取系统时间 var minute = date.getMinutes(); //分 var second = date.getSeconds();//秒 return year + '-' + this.formateDateDounle(month) + '-' + this.formateDateDounle(day) + ' ' + this.formateDateDounle(hour) + ':' + this.formateDateDounle(minute) + ':' + this.formateDateDounle(second) }, /** * 获取日期(年月日) * @param dateDesc * @type yyyy-mm-dd */ getDay(timestamp){ var date = timestamp ? new Date(timestamp) : new Date() var year = date.getFullYear();//获取系统的年; var month = date.getMonth() + 1;//获取系统月份,由于月份是从0开始计算,所以要加1 var day = date.getDate(); //获取系统日 return year + '-' + this.formateDateDounle(month) + '-' + this.formateDateDounle(day) }, /** * 获取后几个小时时间 * @param num 后几小时 * @param timestamp 时间戳 * @type yyyy-mm-dd hh:mm:ss */ getLateTime(num, timestamp){ if (!timestamp) { //不传则为当前时间延迟 timestamp = new Date().getTime() } return this.createTime(timestamp + num * 60 * 60 * 1000) }, /** * 时间转换两位数字 * @param num */ formateDateDounle(num){ return num.toString().length > 1 ? num : '0' + num }, /** * 获取时分 * @param date 格式 yyyy-mm-dd hh:mm:ss * @type hh:mm */ getHourMin(date){ let dateArr = this.getLateTime(1).split(' ')[1].split(':') return dateArr[0] + ':' + dateArr[1]; }
最新回复(0)