js中解析时间戳

xiaoxiao2021-02-27  333

在本人的即时通讯项目中,需要将时间戳转换为时间 规则是:如果是今天的消息,只显示时分 如果不是今天或者本月的消息,显示 月 日 时 分 如果不是本月或本年的消息,显示 年 月 日 时 分

//时间戳转换为时间 function(timestamp){ //获取当前时间 var now=new Date(); //根据指定时间戳转换为时间格式 var time=new Date(); time.setTime(timestamp); //比较当前时间和指定时间的差来决定显示时间格式 //1.年份与当前不同则显示完整日期 yyyy-MM-dd hh:mm if(time.getFullYear() != now.getFullYear()) return time.getFullYear()+"-" +((time.getMonth()+1)<10?"0"+(time.getMonth()+1):(time.getMonth()+1))+"-" +(time.getDate()<10?"0"+time.getDate():time.getDate())+" " +(time.getHours()<10?"0"+time.getHours():time.getHours())+":" +(time.getMinutes()<10?"0"+time.getMinutes():time.getMinutes()); //2.年份与当前相同但月份或日期不同时 显示 MM-dd hh:mm格式 else if(time.getMonth() != now.getMonth() || time.getDate() != now.getDate()) return ((time.getMonth()+1)<10?"0"+(time.getMonth()+1):(time.getMonth()+1))+"-" +(time.getDate()<10?"0"+time.getDate():time.getDate())+" " +(time.getHours()<10?"0"+time.getHours():time.getHours())+":" +(time.getMinutes()<10?"0"+time.getMinutes():time.getMinutes()); //3.年份与日期均与当前相同时,显示hh:mm格式 else return (time.getHours()<10?"0"+time.getHours():time.getHours())+":" +(time.getMinutes()<10?"0"+time.getMinutes():time.getMinutes()); };
转载请注明原文地址: https://www.6miu.com/read-2037.html

最新回复(0)