JS时间格式转换

1.TZ类型的UTC时间转化为本地时间

javascript 复制代码
tz_t_locale = (time: string, time_zone: number) => {
        let T_pos = time?.indexOf('T');
        let Z_pos = time?.indexOf('Z');
        let year_month_day = time?.substr(0,T_pos);
        let hour_minute_second = time?.substr(T_pos+1,Z_pos-T_pos-1);
        time = year_month_day+" "+hour_minute_second;
        return time ?moment(time).subtract(-time_zone, 'hours').local().format('YYYY-MM-DD HH:mm:ss') :'------';
    }

time_zone为时区,中国所在东八区,传8即可。

2.时间戳类型时间转化为本地时间

javascript 复制代码
stamp_t_locale = (time: any) => {
        let date = new Date(time * 1000);
        let year = date.getFullYear();
        let month = (date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}`: date.getMonth() + 1)
        let day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
        let hours = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours();
        let minutes = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes();
        let seconds = date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds();
        let formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        return formattedDate
    }
相关推荐
酒尘&3 小时前
JS数组不止Array!索引集合类全面解析
开发语言·前端·javascript·学习·js
学历真的很重要3 小时前
VsCode+Roo Code+Gemini 2.5 Pro+Gemini Balance AI辅助编程环境搭建(理论上通过多个Api Key负载均衡达到无限免费Gemini 2.5 Pro)
前端·人工智能·vscode·后端·语言模型·负载均衡·ai编程
用户47949283569154 小时前
"讲讲原型链" —— 面试官最爱问的 JavaScript 基础
前端·javascript·面试
用户47949283569154 小时前
2025 年 TC39 都在忙什么?Import Bytes、Iterator Chunking 来了
前端·javascript·面试
大怪v5 小时前
【Virtual World 04】我们的目标,无限宇宙!!
前端·javascript·代码规范
狂炫冰美式5 小时前
不谈技术,搞点文化 🧀 —— 从复活一句明代残诗破局产品迭代
前端·人工智能·后端
xw56 小时前
npm几个实用命令
前端·npm
!win !6 小时前
npm几个实用命令
前端·npm
代码狂想家6 小时前
使用openEuler从零构建用户管理系统Web应用平台
前端
dorisrv8 小时前
优雅的React表单状态管理
前端