ant design的日期时间控件的日期时间格式化方式-moment
将时间戳转化为时间
转换方式1-自定义方法
timestampToTime(str) {
const date = new Date(str);
const Y = `${date.getUTCFullYear()}-`;
const M = `${date.getUTCMonth() + 1 < 10 ? `0${date.getUTCMonth() + 1}` : date.getUTCMonth() + 1}-`;
const D = `${date.getUTCDate()} `;
const h = `${date.getUTCHours() < 10 ? `0${date.getUTCHours()}` : date.getUTCHours()}:`;
const m = `${date.getUTCMinutes() < 10 ? `0${date.getUTCMinutes()}` : date.getUTCMinutes()}:`;
const s = date.getUTCSeconds() < 10 ? `0${date.getUTCSeconds()}` : date.getUTCSeconds();
return Y + M + D + h + m + s;
}
转换方式2-使用moment
npm i moment
dateToString(timestamp) {
return moment(timestamp).format('YYYY-MM-DD HH:mm:ss');
}
将时间转化为时间戳
TimesTamp(str) {
// 获取某个时间格式的时间戳
const stringTime = str;
let timestamp = Date.parse(new Date(stringTime));
timestamp /= 1000;
return timestamp;
}
时间格式化
获取带 - 的日期
formatDate(oDate, str) {
const yy = oDate.getFullYear();
let mm = oDate.getMonth() + 1;
let dd = oDate.getDate();
mm = mm < 10 ? `0${mm}` : mm;
dd = dd < 10 ? `0${dd}` : dd;
const dateStr = `${yy + str + mm + str + dd}`;
return dateStr;
}
获取日期:前天、昨天、今天、明天、后天
GetDateStr(AddDayCount) {
const dd = new Date();
dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期
const y = dd.getFullYear();
const m = dd.getMonth() + 1; // 获取当前月份的日期
const d = dd.getDate();
const today = `${y}-${m}-${d}`;
const defaultQuery = {
endTime: today,
};
return defaultQuery;
}
获取默认今天
// 获取默认今天
getBeforeTodayDate() {
const today = new Date()
.toLocaleDateString()
.split('/')
.join('-');
const defaultQuery = {
endTime: today,
};
return defaultQuery;
},
getToday() {
const todayDate = new Date();
const year = todayDate.getFullYear();
const month = todayDate.getMonth() + 1 > 9 ? `${todayDate.getMonth() + 1}` : `0${todayDate.getMonth() + 1}`;
const day = todayDate.getDate() > 9 ? `${todayDate.getDate()}` : `0${todayDate.getDate()}`;
const today = year + month + day;
return today;
},
getTodayFormat() {
const todayDate = new Date();
const year = todayDate.getFullYear();
const month = todayDate.getMonth() + 1 > 9 ? `${todayDate.getMonth() + 1}` : `0${todayDate.getMonth() + 1}`;
const day = todayDate.getDate() > 9 ? `${todayDate.getDate()}` : `0${todayDate.getDate()}`;
const today = `${year}-${month}-${day}`;
return today;
},
获取上一个工作日
getLastWorkDay() {
const day = moment().day();
const days = day === 1 ? 3 : day === 0 ? 2 : 1; // 周一取值为上周五,周日取值为本周五,其他日期取值为前一天
const lastWorkDay = moment()
.add(-days, 'd')
.format('YYYY-MM-DD');
return lastWorkDay;
}
获取下一个工作日
function getNextWorkDay(dateFormat) {
const day = moment().day();
const days = day !== 5 ? 1 : day === 6 ? 2 : 3;
return moment()
.add(+days, 'd')
.format(dateFormat || 'YYYYMMDD');
}
本文来自博客园,作者:JackieDYH,转载请注明原文链接:https://www.cnblogs.com/JackieDYH/p/17634379.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现