美文网首页
TypeScript中,时间戳转年月日形式显示

TypeScript中,时间戳转年月日形式显示

作者: 全新的饭 | 来源:发表于2022-10-25 10:36 被阅读0次
    // 时间戳(精确到秒,10位)转为'xx年xx月xx日'
    public timestampToDate(timestamp: number): string
    { 
        const date = new Date(timestamp*1000);
        const year = date.getFullYear();
        const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
        const day = date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate();
        const str = `${year}年${month}月${day}日`;

        return str;
    }

相关文章

网友评论

      本文标题:TypeScript中,时间戳转年月日形式显示

      本文链接:https://www.haomeiwen.com/subject/rgvnzrtx.html