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
    }
相关推荐
2301_8127314113 分钟前
CSS3笔记
前端·笔记·css3
ziblog16 分钟前
CSS3白云飘动动画特效
前端·css·css3
越努力越幸运50816 分钟前
CSS3学习之网格布局grid
前端·学习·css3
半斤鸡胗19 分钟前
css3基础
前端·css
ziblog19 分钟前
CSS3创意精美页面过渡动画效果
前端·css·css3
akangznl19 分钟前
第四章 初识css3
前端·css·css3·html5
会豪20 分钟前
深入理解 CSS3 滤镜(filter):从基础到实战进阶
前端·css·css3
头顶一只喵喵21 分钟前
CSS3进阶知识:CSS3盒子模型,box-sizing:content-box和box-sizing:border-box的讲解
前端·css·css3
小飞大王66624 分钟前
css进阶用法
前端·css
普通网友39 分钟前
新手必看!HCCDA-HarmonyOS & Cloud Apps 实验保姆级教程
javascript·angular.js