【taro react】 ---- 通过时间格式化处理提取倒计时的分秒、时段的小时分、选择日期的月和天
1. 前言
需要做一个场次预定的小程序,需要H5和微信小程序两端。在开发中有很多时间需要进行计算处理。返回的时间格式多种多样,还有用作倒计时结束的时间戳等,如何进行统一处理,就是一个需要解决的问题。
2. 将秒转换为 MM:SS 格式
- 这是直接拷贝另一个项目同事的代码,分析一下代码;
- 判断出入的秒数,其实这里应该判断小于等于0时直接返回 00:00;
- 然后将秒数转换为整数;
- 计算分钟数,判断如果小于10就补0;
- 计算秒数,判断如果小于10就补0;
- 最后拼接字符串返回 MM:SS 格式的字符串。
/*** @description 传入秒 转成时分秒 90.00s => */
export const secondsToTime = (seconds) => {// console.log('seconds',seconds);if(seconds == 0) return '00:00'let result = parseInt(seconds);let m = Math.floor((result / 60) % 60) < 10 ? '0' + Math.floor((result / 60) % 60) : Math.floor((result / 60) % 60);let s = Math.floor(result % 60) < 10 ? '0' + Math.floor(result % 60) : Math.floor(result % 60