class Time {
constructor(){
}
/**
* 1.1 时间对象转字符串
* date ---> 2019-03-08 12:12:12
* **/
timeToString( date, format = "yyyy-MM-dd HH:mm:ss" ){
if ( !date ){
return '';
}
//将日期数据,替换成format
let year = date.getFullYear();
let month = date.getMonth()+1;
let day = date.getDate();
let hour = date.getHours();
let minute = date.getMinutes();
let second = date.getSeconds();
format = format.replace( /yyyy|yy|YYYY|yy/, year );
format = format.replace( "MM", month>9 ? month :( '0'+month ) );
format = format.replace( "dd", day>9 ? day : ('0'+day) );
format = format.replace( "HH", hour>9? hour : ('0' + hour) );
format = format.replace( "mm", minute>9 ? minute : ('0'+minute));
format = format.replace( "ss", second>9? second : ('0'+second));
return format;
}
/**
* 1.2 日期转字符串形式
* 比如: Date --> 2019-03-02
* */
dateToString( date, format = "yyyy-MM-dd" ){
if ( !date ){
return '';
}
//将日期数据,替换成format
let year = date.getFullYear();
let month = date.getMonth()+1;
let day = date.getDate();
format = format.replace( /yyyy|yy|YYYY|yy/, year );
format = format.replace( "MM", month>9 ? month :( '0'+month ) );
format = format.replace( "dd", day>9 ? day : ('0'+day) );
return format;
}
/**
* 1.3 获取当前时间的字符串形式
* 比如: 2019-03-08 12:12:12
* */
currentTimeToFormat( format = "yyyy-MM-dd HH:mm:ss" ){
return this.getTimeString(new Date(), format);
}
/**
* 1.4 获取当前日期的字符串形式
* 比如 2019-03-02
* */
currentDateToFormat( format = "yyyy-MM-dd" ){
return this.getDateString(new Date(), format);
}
/**
* 1.5 日期字符串转date对象
* 比如: 2019-03-08 --> date
* 注意:如果直接打印date,此时是去除了时区的时间。
* */
fromatToDate( dateString, format="yyyy-MM-dd HH:mm:ss" ){
if( !dateString ){
return null;
}
dateString = dateString.replace(/\W/g, '-').replace(/-+/g, '-');
return new Date( new Date(dateString).getTime()+new Date().getTimezoneOffset()*1000*60 )
}
/**
* 1.6 时间字符串形式的时间 转换成 date
* 比如: 2019-03-08 12:12:12 --> date
* 注意:如果直接打印date,此时是去除了时区的时间。
* */
formatToTime( dateString, format="yyyy-MM-dd HH:mm:ss" ){
if( !dateString ){
return null;
}
const list = dateString.replace(/\W/g, ':').replace(/:+/g, ':').split(":");
let stringBuild = list.slice(0,3).join("/") +" "+ list.slice(3,6).join(":")
return new Date( stringBuild );
}
/**
* 2.1 Date对象,转成js对象
* */
dateToObject( date ){
if ( !date ){
return {};
}
return {
year : date.getFullYear(),
month: date.getMonth()+1,
day: date.getDate(),
hour: date.getHours(),
minute: date.getMinutes(),
seconds: date.getSeconds()
}
}
/**
* 2.2 时间字符串,转换成js对象
* */
objectToDate( timeObject ){
let stringBuild = "";
stringBuild += (timeObject.year? timeObject.year : '1970') +"/"
stringBuild += (timeObject.month?timeObject.month : '00') + "/"
stringBuild += (timeObject.day?timeObject.day : '00') + " "
stringBuild += (timeObject.hour?timeObject.hour: '00') + ":"
stringBuild += (timeObject.minute?timeObject.minute:'00')+":"
stringBuild += (timeObject.seconds?timeObject.seconds:'00')
return new Date(stringBuild);
}
/**
* 时间戳 转 Date
* */
timestampToDate( timestamp ){
return new Date(timestamp);
}
/**
* 时间戳 转 format
* */
timestampToFormat( timestamp, format="yyyy-MM-dd HH:mm:ss" ){
const date = this.timestampToDate(timestamp);
return this.currentDateToFormat( date, format );
}
/**
* 时间戳 转 JSON对象
* */
timestampToJSON( timestamp ){
const date = this.timestampToDate(timestamp);
return this.dateToObject(date);
}
/**
* date 转 时间戳
* */
dateToTimestamp( date ){
return date.getTime()/1000;
}
}
module.exports = new Time();