【一个小函数】JS将Unix时间戳转换为英文日期格式

将后台返回的Unix时间戳转换成英文日期格式。

//将毫秒的时间转换成美式英语的时间格式,eg:3rd May 2018
function formatDate(millinSeconds){
	var date = new Date(millinSeconds);
	var monthArr = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Spt","Oct","Nov","Dec");
	var suffix = new Array("st","nd","rd","th");
	
	var year = date.getFullYear(); //年
	var month = monthArr[date.getMonth()]; //月
	var ddate = date.getDate(); //日
	
	if(ddate % 10 < 1 || ddate % 10 > 3) {
		ddate = ddate + suffix[3];
	}else if(ddate % 10 == 1) {
		ddate = ddate + suffix[0];
	} else if(ddate % 10 == 2) {
		ddate = ddate + suffix[1];
	}else {
		ddate = ddate + suffix[2];
	}
	return ddate + " "+ month + " " + year;
}

测试代码和结果:

【一个小函数】JS将Unix时间戳转换为英文日期格式_第1张图片

你可能感兴趣的:(web前端,js)