js时间戳日期相互转换

dataFormate(timestamp,type){
                // 时间转化年月日时分秒
                if(type=='time'){
                    let d = new Date(timestamp * 1000);    //根据时间戳生成的时间对象
                    let dateStr = (d.getFullYear()) + "年" +
                        (d.getMonth() + 1) + "月" +
                        (d.getDate()) + "日 " +
                        (d.getHours()>9?d.getHours():('0'+d.getHours())) + ":" +
                        (d.getMinutes()>9?d.getMinutes():('0'+d.getMinutes())) + ":" +
                        (d.getSeconds()>9?d.getSeconds():('0'+d.getSeconds()));
                    return dateStr;
                }else{ //计算时间间隔格式
                    //计算出天数
                    let days=Math.floor(timestamp/(24*3600));
                    //计算出小时数
                    let leave1=timestamp%(24*3600);
                    let hours=Math.floor(leave1/3600);
                    //计算相差分钟数
                    let leave2=leave1%(3600);
                    let minutes=Math.floor(leave2/60);
                    //计算相差秒数
                    let seconds=leave2%(60);

                    let daysStr = days>0 ? days+'天':'';
                    let hoursStr = hours>0 ? hours+'时':'';
                    let minutesStr = minutes>0 ? minutes+'分':'';
                    let secondsStr = minutes>0 ? minutes+'秒':'';

                    return daysStr+hoursStr+minutesStr+secondsStr;
                }

            }

你可能感兴趣的:(javascript)