JS怎么转换日期格式

参数checktime的类型是date,格式是13位数字的时间戳,

我想把它转为timestamp类型,格式转成2021-04-17 17:57:00.000000这种格式

请问大佬们应该怎么写

var date = new Date(你转换的时间);
var d = date.getFullYear() + "-" + (Number(date.getMonth())+Number(1)) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
alert(d);

function getTimeStr(d, t) {
    if (d) {
        switch (d.toString().length) {
            case 10:
                d = d * 1000;
                break;
            case 13:
                d = d * 1;
                break;
            default:
                break;
        }
        var now = new Date(parseInt(d));
        var year = now.getFullYear();
        var month = (now.getMonth() + 1) < 10 ? '0' + (now.getMonth() + 1) : (now.getMonth() + 1);
        var date = now.getDate() < 10 ? '0' + now.getDate() : now.getDate();
        var hours = now.getHours() < 10 ? '0' + now.getHours() : now.getHours();
        var minutes = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes();
        var seconds = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds();
        if (t) {
            if (t == 1) {
                return year + '/' + month + '/' + date + ' ' + hours + ':' + minutes + ':' + seconds;
            } else if (t == 2) {
                return year + '年' + month + '月' + date + '日' + ' ' + hours + ':' + minutes + ':' + seconds;
            } else if (t == 3) {
                return year + ' . ' + month + ' . ' + date;
            } else if (t == 4) {
                return hours + ':' + minutes;
            } else if (t == 5) {
                return year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + seconds;
            } else if (t == 6) {
                return year + '-' + month + '-' + date;
            }
        } else {
            return year + ' 年 ' + month + ' 月 ' + date + ' 日 ';
        }
    }
}

/*
* 时间戳转日期。
* @param d : 时间戳
* @param t : 日期格式
*/

moment 了解一下

推荐使用moment.js 专门处理时间的js库

建议使用moment.js,处理日期格式

 

//引入
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

<script>
//new Date().getTime() 可以修改为你得13位时间戳
var date = moment(new Date().getTime()).format("YYYY-MM-DD HH:mm:ss:SSSSSS")
console.log(date) // "2021-04-26 09:26:10:185000"
</script>