我们 a 组组长发了一张图过来,说是为什么之前把 dateModeTransform.js 处理日期的改了,让我改回来,说是要用到 年月日时分秒,还有之前的写法也有很多不妥的地方就是传递的参数太多了,这次简化了一下
![dd29bab22febb9a3e941b6488a18c9f5.png](https://img-blog.csdnimg.cn/img_convert/dd29bab22febb9a3e941b6488a18c9f5.png)
之前的代码如下:
// 时间戳转换方法
/**
* @params timestamp, 传入需要 转换的时间戳,
* @params format, 默认为 "ymd", 代表 年月日, 'ymdhms' 代表 年月日时分秒, 'ymdhm' 代表 年月日时分, 'hm' 代表 时分 String
* @content 如果只传递 timestamp 默认返回 年月日,
* @return dateime
* @author Ken
* */
function padZero(num) {
if(num < 10) {
return `0${num}`
} else {
return num
}
}
const dateModeTransform = {
dateModeTransform(timestamp, yBool=false, mBool=false, dBool=false, hBool=false, mBool=false, fBool=false, sBool=false) {
let datetime = new Date(timestamp)
let year = datetime.getFullYear()
let month = padZero(datetime.getMonth() + 1)
let date = padZero(datetime.getDate())
let hours = padZero(datetime.getHours())
let minutes = padZero(datetime.getMinutes())
let seconds = padZero(datetime.getSeconds())
if(yBool && mBool && dBool && hBool && mBool && sBool) {
// 年月日时分秒
return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`
}
if(yBool && mBool && dBool && hBool && mBool) {
// 年月日时分
return `${year}-${month}-${date} ${hours}:${minutes}`
}
if(yBool && mBool && dBool) {
// 年月日
return `${year}-${month}-${date}`
}
if(hBool && mBool && sBool) {
// 时分秒
return `${hours}:${minutes}:${seconds}`
}
if(hBool && mBool) {
// 时分
return `${hours}:${minutes}`
}
if(hBool) {
// 时
return `${hours}`
}
// 默认返回 年月日
return `${year}-${month}-${date}`
}
}
export default dateModeTransform
今天一看,挖草,咋写成这样了,我调一个方法传递的参数这么麻烦,写一堆 bool 值在哪里,也不知道这些布尔值有什么含义,这是垃圾代码,今天调整了一下代码的传递参数格式之后,代码简洁了很多。
今天修改了一下传递参数的方式
// 时间戳转换方法
/**
* @params timestamp, 传入需要 转换的时间戳,
* @params format, 默认为 "ymd", 代表 年月日, 'ymdhms' 代表 年月日时分秒, 'ymdhm' 代表 年月日时分, 'hm' 代表 时分 String
* @content 如果只传递 timestamp 默认返回 年月日,
* @return dateime
* @author Ken
* */
function padZero(num) {
if(num < 10) {
return `0${num}`
} else {
return num
}
}
// 日期以及时间格式常量定义
const CONSTVARIABLE = {
YMDHMS: "yyyy-mm-dd hh:mm:ss", // 年月日时分秒
YMDHM: "yyyy-mm-dd hh:mm", // 年月日时分
YMD: "yyyy-mm-dd", // 年月日
HMS: "hh:mm:ss", // 时分秒
HM: "hh:mm", // 时分
H: "hh" // 时
}
const dateModeTransform = {
dateModeTransform(timestamp, format=CONSTVARIABLE.YMD) {
let datetime = new Date(timestamp)
let year = datetime.getFullYear()
let month = padZero(datetime.getMonth() + 1)
let date = padZero(datetime.getDate())
let hours = padZero(datetime.getHours())
let minutes = padZero(datetime.getMinutes())
let seconds = padZero(datetime.getSeconds())
if(format === CONSTVARIABLE.YMDHMS) {
// 年月日时分秒
return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`
}
if(format === CONSTVARIABLE.YMDHM) {
// 年月日时分
return `${year}-${month}-${date} ${hours}:${minutes}`
}
if(format === CONSTVARIABLE.YMD) {
// 年月日
return `${year}-${month}-${date}`
}
if(format === CONSTVARIABLE.HMS) {
// 时分秒
return `${hours}:${minutes}:${seconds}`
}
if(format === CONSTVARIABLE.HM) {
// 时分
return `${hours}:${minutes}`
}
if(format === CONSTVARIABLE.H) {
// 时
return `${hours}`
}
// 默认返回 年月日
return `${year}-${month}-${date}`
}
}
export default dateModeTransform
首先,我这里学到了一个很重要的技巧,就是比如我们事先已经知到有哪几种情况就像上面日期处理函数那样,比如用户可能处理 年月日时分秒,年月日时分,年月日,时分秒,时分,时这几种情况,那么我们可以使用一些 const 变量来事先定义好,然后用户传递进来的参数根据写好的东西进行判断就好了
![983bef6796f09dce74ac8b54273ecd02.png](https://img-blog.csdnimg.cn/img_convert/983bef6796f09dce74ac8b54273ecd02.png)