フォーマットjavascript日付

1126 ワード

/**     */
var time = {};
time.formatTime = function(time){
	if(typeof time == "number"){
		time = new Date(time);
	}
	var hour = time.getHours();
	if(hour<10){
		hour = "0"+hour;
	}
	var minute = time.getMinutes();
	if(minute<10){
		minute = "0"+minute;
	}
	var second = time.getSeconds();
	if(second<10){
		second = "0"+second;
	}
	var temp = hour+":"+minute+":"+second;
	return temp;
};

time.formatDate = function(date,flag){
	if(typeof time == "number"){
		time = new Date(time);
	}
	var temp = "";
	var month = date.getMonth()+1;
	if(month<10){
		 month = "0"+month;
	}
	var day = date.getDate();
	if(day<10){
		day = "0"+day;
	}
	if(flag){
		temp = date.getFullYear()+"-"+month+"-"+day;
	}else{
		temp = date.getFullYear()+month+day;
	}
	return temp;
};

/**         */
time.formatTimestamp = function(timestamp){
	if(typeof timestamp == "number") timestamp = new Date(timestamp);
	return this.formatDate(timestamp, true)+" "+this.formatTime(timestamp);
};