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
    }
相关推荐
niucloud-admin1 小时前
web 端前端
前端
摘星编程4 小时前
React Native for OpenHarmony 实战:Linking 链接处理详解
javascript·react native·react.js
胖者是谁5 小时前
EasyPlayerPro的使用方法
前端·javascript·css
EndingCoder5 小时前
索引类型和 keyof 操作符
linux·运维·前端·javascript·ubuntu·typescript
liux35285 小时前
Web集群管理实战指南:从架构到运维
运维·前端·架构
沛沛老爹5 小时前
Web转AI架构篇 Agent Skills vs MCP:工具箱与标准接口的本质区别
java·开发语言·前端·人工智能·架构·企业开发
摘星编程5 小时前
React Native for OpenHarmony 实战:ImageBackground 背景图片详解
javascript·react native·react.js
小光学长6 小时前
基于Web的长江游轮公共服务系统j225o57w(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
前端·数据库
摘星编程7 小时前
React Native for OpenHarmony 实战:Alert 警告提示详解
javascript·react native·react.js
Joe5567 小时前
vue2 + antDesign 下拉框限制只能选择2个
服务器·前端·javascript