时间戳转为日期格式

在线时间戳转换工具
在java中获取时间戳方式:

//获取现在的秒级别的时间戳
long time = System.currentTimeMillis() / 1000;
//获取现在的毫秒级别的时间戳
long time = System.currentTimeMillis();

下面方法中,在调用Date构造函数而不传递参数的情况下,新创建的对象自动获得当前日期和时间。如果想传入特定日期,需将表示日期的字符串传递给Date构造函数。

//时间戳转为时间格式yyyy-MM-dd HH:mm:ss
    function getDateTimeToDate(a) {
        //这里接收的是一个String类型的数值
        var now;
        //当时间戳小于1000000000000时代表是秒级别的时间戳
        if (parseInt(a) < 1000000000000) {
            now = new Date(parseInt(a) * 1000);
        } else {
            now = new Date(parseInt(a));
        }
        var year = now.getFullYear() < 10 ? '0' + now.getFullYear() : now.getFullYear();  //取得4位数的年份
        var month = now.getMonth() + 1 < 10 ? '0' + (now.getMonth() + 1) : now.getMonth() + 1;  //取得日期中的月份,其中0表示1月,11表示12月
        var date = now.getDate() < 10 ? '0' + now.getDate() : now.getDate();      //返回日期月份中的天数(1到31)
        var hour = now.getHours() < 10 ? '0' + now.getHours() : now.getHours();    //返回日期中的小时数(0到23)
        var minute = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes();//返回日期中的分钟数(0到59)
        var second = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds();//返回日期中的秒数(0到59)
        return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
    }

你可能感兴趣的:(时间戳转为日期格式)