//, 时间格式化输出,如03:25:19 86。每10ms都会调用一次
function dateFormatHMS(micro_second) {
// 秒数
var second = Math.floor(micro_second / 1000);
// 小时位
var hr = fillZeroPrefix(Math.floor(second / (60 * 60)));
second = second - hr * 60 * 60
// 分钟位
var min = fillZeroPrefix(Math.floor(second / 60));
second = second - min * 60
// 秒位
var sec = fillZeroPrefix((second)); // equal to => var sec = second % 60;
// 毫秒位,保留2位
var micro_sec = fillZeroPrefix(Math.floor((micro_second % 1000) / 10));
var time
if (day > 0) {
time = day + '天' + hr + "小时" + min + "分" + sec + "秒";
} else if( hr > 0){
time = hr + "小时" + min + "分" + sec + "秒";
}else{
time = min + "分" + sec + "秒";
}
return time;
};
// 位数不足补零
function fillZeroPrefix(num) {
return num < 10 ? "0" + num : num
}
function dateFormatYMD(longTypeDate) {
var datetimeType = "";
var date = new Date();
date.setTime(longTypeDate);
datetimeType += date.getFullYear(); //年
datetimeType += "-" + getMonth(date); //月
datetimeType += "-" + getDay(date); //日
return datetimeType;
}
function dateFormatYMDHMS(longTypeDate) {
var datetimeType = "";
var date = new Date();
date.setTime(longTypeDate);
datetimeType += date.getFullYear(); //年
datetimeType += "-" + getMonth(date); //月
datetimeType += "-" + getDay(date); //日
datetimeType += " " + getHours(date); //时
datetimeType += ":" + getMinutes(date); //分
datetimeType += ":" + getSeconds(date); //秒
return datetimeType;
}
//返回 01-12 的月份值
function getMonth(date) {
var month = "";
month = date.getMonth() + 1; //getMonth()得到的月份是0-11
if (month < 10) {
month = "0" + month;
}
return month;
}
//返回01-30的日期
function getDay(date) {
var day = "";
day = date.getDate();
if (day < 10) {
day = "0" + day;
}
return day;
}
//返回小时
function getHours(date) {
var hours = "";
hours = date.getHours();
if (hours < 10) {
hours = "0" + hours;
}
return hours;
}
//返回分
function getMinutes(date) {
var minute = "";
minute = date.getMinutes();
if (minute < 10) {
minute = "0" + minute;
}
return minute;
}
//返回秒
function getSeconds(date) {
var second = "";
second = date.getSeconds();
if (second < 10) {
second = "0" + second;
}
return second;
}
//秒 转 天 时 分 秒
function secondDHMS( second_time ){
var time =fillZeroPrefix( parseInt(second_time) )+ " ";
if( parseInt(second_time )>= 60){
var second =fillZeroPrefix( parseInt(second_time) % 60);
var min =fillZeroPrefix( parseInt(second_time / 60));
time = min + "分" + second + "秒";
if( min >= 60 ){
min =fillZeroPrefix( parseInt(second_time / 60) % 60);
var hour = parseInt( parseInt(second_time / 60) /60 );
time = hour + "小时" + min + "分" + second + "秒";
if( hour >= 24 ){
hour = parseInt( parseInt(second_time / 60) /60 ) % 24;
var day = parseInt( parseInt( parseInt(second_time / 60) /60 ) / 24 );
time = day + "天" + hour + "小时" + min + "分" + second + "秒";
}
}
}
return time;
}
module.exports = {
dateFormatHMS,
dateFormatYMD,
dateFormatYMDHMS,
secondDHMS
}