时间戳 转 年月日

321 阅读1分钟

方法1

/**
 * 
 * @param {*} time  时间戳   单位是秒
 * @returns 2002-12-12
 */

export const getTheDate = function(time) {
    time = time * 1000;
    var date = new Date(time);
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    return [year, month, day].map(formatNumber).join('-')
}




function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}

方法2

	Date.prototype.format = function(fmt) {
		let o = {
			"M+": this.getMonth() + 1, //月份 
			"d+": this.getDate(), //日 
			"h+": this.getHours(), //小时 
			"m+": this.getMinutes(), //分 
			"s+": this.getSeconds(), //秒 
		};
		if (/(y+)/.test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
		}
		for (let k in o) {
			if (new RegExp("(" + k + ")").test(fmt)) {
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[
					k]).length)));
			}
		}
		return fmt;
	}