Date.prototype.Format = function(fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var 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;
}

export function curDate( ) {
    var d = new Date().Format("yyyy-MM-dd");
    return d.toLocaleString();
}

export function formatTimeToStr(times, pattern) {
    var d = new Date(times).Format("yyyy-MM-dd hh:mm:ss");
    if (pattern) {
        d = new Date(times).Format(pattern);
    }
    return d.toLocaleString();
}

export function toDatetime(times ) {
	if( !times) return "2020-01-01 00:00:00";
    if(Number(times)) times = times*1000;

    var d = new Date(times).Format("yyyy-MM-dd hh:mm:ss");
    return d.toLocaleString();
}

export function toDate(times ) {
	if( !times) return "2020-01-01"
    if(Number(times) && times < 3656594008) times = times*1000;
    var d = new Date(times).Format("yyyy-MM-dd");
    return d.toLocaleString();
}

export function toDateShow(times ) {
	if( !times) return "---"
    if(Number(times)) times = times*1000;
    var d = new Date(times).Format("yyyy-MM-dd");
    return d.toLocaleString();
}

export function getDate( days ) {
    var d = new Date();
    d.setDate(d.getDate() + days);
    return toDate( d )
}

export function time2str( mt ){
  let  timestamp4 = new Date( mt*1000 );
  return timestamp4.toLocaleDateString().replace(/\//g, "-") + " " + timestamp4.toTimeString().substr(0, 8);
}

export function str2time( str ){
  let  timestamp4 = new Date( str );
  return parseInt(timestamp4.getTime()/1000);
}

export function getTime(){
    return parseInt( Date.now()/1000);
}

export function packTime( mytime ){
    if( mytime < 0) mytime = 0;
    let obj = {v: mytime};
	  obj.d = parseInt(mytime / 86400);
    obj.h =  parseInt( (mytime %86400)/ 3600);
    obj.m =  parseInt( (mytime %3600)/60);
    obj.s = mytime % 60;
    return obj;
}
