• 各类 动态获取时间,计算距离,贝塞尔曲线等 的方法


    1,获取年月日 时分秒

    setInterval(function(){

      setTime();

    },1000);

    private setTime() { 
    var myDate: Date = new Date();
    var year = myDate.getFullYear();
    var month :string; 
    var date :string; 
    var hour :string; 
    var minute :string; 
    var second :string;
    
    if (myDate.getMonth() < 10 && myDate.getMonth() > 0) {
    month = "0" + (myDate.getMonth() + 1);
    }else{
    month = (myDate.getMonth() + 1).toString();
    }
    
    if ( myDate.getDate() < 10 && myDate.getDate() > 0) {
    date = "0" + myDate.getDate();
    }else{
    date = (myDate.getDate()).toString();
    }
    
    if (myDate.getHours() < 10 && myDate.getHours() >= 0) {
    hour = "0" + myDate.getHours();
    }else{
    hour = (myDate.getHours()).toString();
    }
    
    if (myDate.getMinutes() < 10 && myDate.getMinutes() >= 0) {
    minute = "0" +myDate.getMinutes();
    }else{
    minute = (myDate.getMinutes()).toString();
    }
    
    if ( myDate.getSeconds() < 10 && myDate.getSeconds() >= 0) {
    second = "0" + myDate.getSeconds();
    }else{
    second = (myDate.getSeconds()).toString();
    }
    
    this.worldTime.text = "世界时间 " + year + "-" + month + "-" + date + " " + hour+ ":" + minute + ":" + second;
    
    }
    

     2,

    module utils {
        export class Utils extends egret.HashObject {
            public static _instance: Utils;
            private _libs: any = {};
            protected _assetsLoaded: boolean = false;
            public static assetsUrl: string;
    
            public constructor() {
                super();
            }
    
            public static getInstance(): Utils {
                if (!Utils._instance) {
                    Utils._instance = new Utils();
                }
                return (utils.Utils._instance);
            }
    
            public static removeAllChild(con: egret.Sprite) {
                while (con.numChildren > 0)
                    con.removeChildAt(0);
            }
    
            public static setBtnEnable(btn: eui.Button, enable: boolean, tip: any = null) {
                btn.enabled = enable;
                if (enable) {
                    btn.touchEnabled = true;
                    btn.filters = null;
                    if (tip)
                        utils.Tip.getInstance().addTarget(btn, tip);
                    else
                        utils.Tip.getInstance().removeTarget(btn);
                }
                else {
                    if (tip) {
                        btn.touchEnabled = true;
                        utils.Tip.getInstance().addTarget(btn, tip);
                    }
                    else {
                        btn.touchEnabled = false;
                        utils.Tip.getInstance().removeTarget(btn);
                    }
                    btn.filters = [utils.FilterUtil.filter1];
                }
            }
    
            //public static genBitMapByTF(tf: egret.TextField): egret.Bitmap {
            //var bmd: flash.BitmapData = new flash.BitmapData(tf.width, 25, true, 0);
            //  var bmd: egret.BitmapData = new egret.BitmapData(null, tf.width, 25);
            //  bmd.draw2(tf, null, null, null, null, true);
            //  return new egret.Bitmap(bmd);
            //  }
    
            public static initSearchTF(tf: egret.TextField, prompt: string, doSearch: Function, needAutoSearch: boolean = true) {
                // tf.type = "input";
                // tf["selectable"] = true;
                // tf.touchEnabled = true;
                // tf.maxChars = 23;
                // tf.text = prompt;
                // tf.addEventListener(egret.FocusEvent.FOCUS_IN, onSearchFocus, null);
                // tf.addEventListener(egret.FocusEvent.FOCUS_OUT, onSearchFocus, null);
                // function onSearchFocus(event: egret.FocusEvent) {
                //     if (event.type == egret.FocusEvent.FOCUS_IN && tf.text == prompt) {
                //         tf.text = "";
                //     }
                //     else if (event.type == egret.FocusEvent.FOCUS_OUT && utils.StringUtil.trim(tf.text) == "") {
                //         tf.text = prompt;
                //     }
                // };
                // var timerId: number = Number.MAX_VALUE;
                // if (needAutoSearch) {
                //     tf.addEventListener(egret.Event.CHANGE, onSearch, null);
                //     function onSearch(event: egret.Event) {
                //         egret.clearTimeout(timerId);
                //         timerId = egret.setTimeout(doSearch, this, 1000);
                //     };
                // }
            }
    
            public static deleteItem(from: any, value: any) {
                for (var key in from) {
                    if (from[key] == value)
                        delete from[key];
                }
            }
    
            public static removeValue(arr: any, value: any) {
                for (var i: number = arr.length - 1; i > -1; i--) {
                    if (arr[i] === value) {
                        arr.splice(i, 1);
                    }
                }
            }
    
            public static obj2array(obj: any, ary: Array<any> = null): Array<any> {
                if (obj == null)
                    return ary;
                ary = ary || [];
                ary.length = 0;
                for (var o_key_a in obj) {
                    var o: Object = obj[o_key_a];
                    ary.push(o);
                }
                return ary;
            }
    
            public static getTimeHMSAuto(second: number): string {
                //second = flash.checkInt(second);
                if (second > 3600)
                    return Utils.getTimeHMS(second);
                else
                    return Utils.getTimeMS(second);
            }
    
            public static getTimeHMS(second: number): string {
                //second = flash.checkInt(second);
                var hor: number = second / 3600;
                var min: number = (second / 60) % 60;
                var sec: number = second % 60;
                return (hor > 9 ? "" : "0") + hor + ":" + (min > 9 ? "" : "0") + min + ":" + (sec > 9 ? "" : "0") + sec;
            }
    
            public static getTimeHM(second: number): string {
                //second = flash.checkInt(second);
                var hor: number = second / 3600;
                var min: number = (second / 60) % 60;
                return (hor > 9 ? "" : "0") + hor + ":" + (min > 9 ? "" : "0") + min;
            }
    
            public static getTimeMS(second: number): string {
                //second = flash.checkInt(second);
                var hour: number = second / 3600;
                var min: number = (second / 60) % 60;
                var sec: number = second % 60;
                return (hour > 0 ? hour + ":" : "") + (min > 9 ? "" : "0") + min + ":" + (sec > 9 ? "" : "0") + sec;
            }
    
            public static getTimeStamp(second: number): string {
                //second = flash.checkUint(second);
                var hor: number = second / 3600;
                var min: number = (second - hor * 3600) / 60;
                var sec: number = second % 60;
                var stamp: string = "";
                if (hor > 0) {
                    //stamp += hor + AppConfig.text.getValue('timeHour');
                    if (min > 0) {
                        // stamp += min + AppConfig.text.getValue('timeMinute');
                        if (sec > 0) {
                            // stamp += sec + AppConfig.text.getValue('timeSencond');
                        }
                    }
                }
                return stamp;
            }
    
            public static setSimpleButtonText(button: eui.Button, upText: string, downText: string = null, overText: string = null) {
                if (button && upText) {
                    downText = downText || upText;
                    overText = overText || upText;
                    var tf: egret.TextField = null;
                    //tf = getTf(flash.As3As(button.upState, egret.Sprite));
                    //  tf = getTf(button.upState as egret.Sprite);
    
                    if (tf)
                        tf.text = upText;
                    // tf = getTf(flash.As3As(button.downState, egret.Sprite));
                    // tf = getTf(button.downState as egret.Sprite);
                    if (tf)
                        tf.text = downText;
                    // tf = getTf(button.overState as egret.Sprite);
                    if (tf)
                        tf.text = overText;
                }
                function getTf(sprite: egret.Sprite): egret.TextField {
                    if (sprite == null)
                        return null;
                    for (var i: number = 0; i < sprite.numChildren; i++) {
                        if (sprite.getChildAt(i) as egret.TextField)
                            //return flash.As3As(sprite.getChildAt(i), egret.TextField);
                            return sprite.getChildAt(i) as egret.TextField;
    
                    }
                    return null;
                };
            }
    
            public static getFileName(url: string, withPath: boolean = true, withExName: boolean = true): string {
                var fileName: string = url;
                fileName = fileName.split('?').shift();
                fileName = fileName.split('//').pop();
                if (!withPath)
                    fileName = fileName.split('/').pop();
                if (!withExName) {
                    var buf: Array<any> = fileName.split('.');
                    buf.pop();
                    fileName = buf.join(".");
                }
                return fileName;
            }
    
            public static convertDisplayObjectToBitmap(o: egret.DisplayObject): egret.Bitmap {
                //if (flash.As3is(o, egret.Bitmap))
                if (o instanceof egret.Bitmap)
                    //  return flash.As3As(o, egret.Bitmap);
                    return o as egret.Bitmap;
    
                //   var bmd: egret.BitmapData = new egret.BitmapData(o.width, o.height, true, 0x00ffffff);
                // bmd.draw2(o);
                //   return new egret.Bitmap(bmd);
            }
    
            public loadResources(param1: Array<any>, param2: LoadResponder, param3: boolean = false) {
                var _self__: any = this;
                var list: Array<any> = param1;
                var loadResponder: LoadResponder = param2;
                var fullUrl: boolean = param3;
                var callback: Function = function (param1: Array<any>) {
                    oObject.list(param1, _self__._libs, list);
                    loadResponder.lCallback();
                };
                var progress: Function = function (param1: number, param2: number, param3: number, param4: string) {
                    loadResponder.lProgress('progress str', param3, param2, param1, param4);
                };
                var tempList: Array<any> = [];
                var len: number = list.length;
                var i: number = 0;
                while (i < len) {
                    tempList[i] = fullUrl ? (list[i]) : (this.getAssetsUrl(list[i]));
                    ++i;
                }
                FileUtil.loadList(tempList, callback, progress);
            }
    
            public hasResource(param1: string): boolean {
                return this._libs[param1];
            }
    
            public removeResource(param1: string) {
                if (this.hasResource(param1)) {
                    delete this._libs[param1];
                }
            }
    
            public getAssetsObj(file: string): File {
                //   return flash.As3As(this._libs[file], File);
                return this._libs[file] as File;
    
            }
    
            public addLib(fileName: string, file: File) {
                this._libs[fileName] = file;
            }
    
            public getAssetsClass(file: string, className: string): any {
                if (false == this.hasResource(file))
                    //throw new flash.Error(file + 'is not loaded').message;
                    new Error(file + 'is not loaded').message;
                //   return (this._libs[file] as File).getClassByName(className);
            }
    
            public getAssetsUrl(param1: string): string {
                if (param1.indexOf(Utils.assetsUrl) == 0) {
                    return param1;
                }
                return Utils.assetsUrl + param1;
            }
    
            public loadAssets(sign: string, callback: Function, description: string = "", hideLoading: boolean = false) {
                var self: any = this;
                var lr: LoadResponder;
                var handler: Function = function () {
                    callback();
                };
                if (this.hasResource(sign)) {
                    handler();
                }
                else {
                    lr = new LoadResponder(handler, function (param1: string, param2: number) {
                    });
                    this.loadResources([sign], lr);
                }
            }
    
            public getAssetsObject(file: string, className: string): any {
                var classDef: any = this.getAssetsClass(file, className);
                var k: any = new classDef();
                return k;
            }
    
            public static platformNames = {
            "0": "facebook",
            "1": "googleplay",
            "2": "appstore"
            }
    
            //全局字体颜色表--可以扩展 白色 绿色 蓝色 红色  黄色 橙色
            public static ItemColors = {
                "1": 0xe7e7e7,
                "2": 0x19b824,
                "3": 0x4292e4,
                "4": 0xb30f00,
                "5": 0xFFD700,
                "6": 0xe7a118,
                "7": 0xe7a118,
            }
    
            public static ItemColorsString = {
                "1": "#e7e7e7",
                "2": "#19b824",
                "3": "#4292e4",
                "4": "#b30f00",
                "5": "#FFD700",
                "6": "#e7a118",
                "7": "#e7a118",
            }
    
            //战斗面板用颜色字符串 跟上面的有差别
            public static ItemColorsStringFight = {
                "1": "#fef3e7",
                "2": "#1a7a20",
                "3": "#1419ec",
                "4": "#8423a4",
                "5": "#9a4b0a",
                "6": "#b30f00",
                "7": "#b30f00"
            }
            public static TextColors = {
                white: 0xf8f7f6,//白色
                milkWhite: 0xfff9e8,//乳白色 人物等级 
                grayWhite: 0xc8c6c1,//灰白色
                lightWhite: 0xffe3b6,//人物名称
                gray: 0x8e8990,//灰色
                black: 0x010000,//黑色
                yellow: 0xe7a118,//金黄色 
                lightYellow: 0xffe3b6,//淡黄色
                orangeYellow: 0xf2c84e,//橘黄色//道具名称 //玩家姓名
                red: 0xb30f00,//红色
                redTip: 0xff0000,//提示红色
                green: 0x19b824,//绿色 
                blue: 0x1419ec,//蓝色 
                grayBlue: 0x009cb3,//墨蓝色 
                skyBlue: 0x5db2d5,//天空蓝
                purple: 0x7524b6,//紫色
                lightPurple: 0xd049fc,//淡紫色 
                grayPurlple: 0x9175ab,//灰紫色
                pink: 0xdf7dfb,//粉色 
                golden: 0xFFD700 //金色
            }
            public static TextColorsString = {
                white: "#f8f7f6",//白色
                milkWhite: "#fff9e8",//乳白色 人物等级 
                grayWhite: "#c8c6c1",//灰白色
                lightWhite: "0xffe3b6",//人物名称
                gray: "#8e8990",//灰色
                black: "#010000",//黑色
                yellow: "#e7a118",//金黄色 
                lightYellow: "#ffe3b6",//淡黄色
                orangeYellow: "#f2c84e",//橘黄色//道具名称 //玩家姓名
                red: "#b30f00",//红色
                redTip: "#ff0000",//提示红色
                green: "#19b824",//绿色 
                blue: "#1419ec",//蓝色 
                grayBlue: "#009cb3",//墨蓝色 
                skyBlue: "#5db2d5",//天空蓝
                purple: "#7524b6",//紫色
                lightPurple: "#d049fc",//淡紫色 
                grayPurlple: "#9175ab",//灰紫色
                pink: "#df7dfb",//粉色 
                golden: "#FFD700"//金色
            }
            //loadComlete 异步加载才有
            public static createBitmapByName(name: string, smooth: boolean = false, loadComlete: Function = null): egret.Bitmap
            {
                var result: egret.Bitmap = new egret.Bitmap();
                var texture: egret.Texture = RES.getRes(name);
                if (!texture)
                    RES.getResAsync(name, finishFun, this);
                else
                    result.texture = texture;
                function finishFun(data:any,key:string): void
                {
                    result.texture = data;
                    if (loadComlete)
                        loadComlete(data);
                } 
                result.smoothing = smooth;
                return result;
            }
            
            public static createImageByName(name: string, smooth: boolean = false, loadComlete: Function = null):eui.Image
            {
                var result: eui.Image = new eui.Image();
                var texture: egret.Texture = RES.getRes(name);
                if (!texture)
                    RES.getResAsync(name, finishFun, this);
                else
                    result.source = texture;
                function finishFun(data:any,key:string): void
                {
                    result.texture = data;
                    if (loadComlete)
                        loadComlete(data);
                } 
                result.smoothing = smooth;
                return result;
            }
    
            //将 ttable的属性复制到 otable里去
            public static tableReset(otable: Object, ttable: Object, deep: boolean = true): void {
                for (var key in ttable) {
                    if (deep && typeof (ttable[key]) == "object") {
                        var obj: any
                        if (ttable[key] instanceof Array)
                            obj = []
                        else
                            obj = {}
                        utils.Utils.tableReset(obj, ttable[key])
                        otable[key] = obj
                    }
                    else
                        otable[key] = ttable[key]
                }
            }
            public static timeToSecond(time: number): string {
                var h: String;
                var m: String;
                var s: String;
                var str: String;
                var date: Date = new Date(time * 1000);
                if (date.getHours() < 10)
                    h = "0" + date.getHours();
                else h = date.getHours() + "";
                if (date.getMinutes() < 10)
                    m = "0" + date.getMinutes();
                else m = date.getMinutes() + "";
                if (date.getSeconds() < 10)
                    s = "0" + date.getSeconds();
                else s = date.getSeconds() + "";
                return h + ":" + m + ":" + s;
            }
            //spin倒计时
            //time 毫秒
            public static timeToShow(time: number): string
            {
                var str: string = "";
                var _minite: number;
                var day: number = 0;
                var hour: number = 0;
                var minite: number = 0;
                var second: number = 0;
    
                time = Math.ceil(time / 1000);
    
                second = Math.floor(time % 60);
                _minite = Math.floor((time - second) / 60);
                minite = Math.floor(_minite % 60);
                hour = Math.floor((_minite - minite) / 60);
                day = Math.floor(hour / 24);
    
                if (second < 10)
                {
                    var secondString: String;
                    secondString = "0" + second.toString();
                }
                else
                {
                    secondString = second.toString();
                }
                if (minite < 10)
                {
                    var miniteString: String;
                    miniteString = "0" + minite.toString();
                }
                else
                {
                    miniteString = minite.toString();
                }
                if (hour % 24 < 10)
                {
                    var hourString: String;
                    hourString = "0" + (hour % 24).toString();
                }
                else
                {
                    hourString = (hour % 24).toString();
                }
                if (hour >= 24)
                {
                    str = day.toString() + ":"
                        + hourString + ":"
                        + miniteString;
                }
                else
                {
                    if (hour > 0)
                    {
                        str = hourString + ":"
                            + miniteString;
                    }
                    else
                    {
                        str = miniteString + ":"
                            + secondString;
                    }
                }
                return str;
            }
            public static timeToDay(time:number): number
            {
                var _minite: number;
                var day: number = 0;
                var hour: number = 0;
                var minite: number = 0;
                var second: number = 0;
    
                second = Math.floor(time % 60);
                _minite = Math.floor((time - second) / 60);
                minite = Math.floor(_minite % 60);
                hour = Math.floor((_minite - minite) / 60);
                day = Math.floor(hour / 24);
                return day;
            }
            //多余1天的显示**天**小时**分, 多余一小时的显示**小时**分,少于一小时的显示**分**秒 
            public static timeSecondsToSimplify(time: number): string {
                var str: string = "";
                var _minite: number;
                var day: number = 0;
                var hour: number = 0;
                var minite: number = 0;
                var second: number = 0;
    
                second = Math.floor(time % 60);
                _minite = Math.floor((time - second) / 60);
                minite = Math.floor(_minite % 60);
                hour = Math.floor((_minite - minite) / 60);
                day = Math.floor(hour / 24);
    
                if (second < 10) {
                    var secondString: String;
                    secondString = "0" + second.toString();
                }
                else {
                    secondString = second.toString();
                }
                if (minite < 10) {
                    var miniteString: String;
                    miniteString = "0" + minite.toString();
                }
                else {
                    miniteString = minite.toString();
                }
                if (hour % 24 < 10) {
                    var hourString: String;
                    hourString = "0" + (hour % 24).toString();
                }
                else {
                    hourString = (hour % 24).toString();
                }
                if (hour >= 24) {
                    str = day.toString() + "天"
                        + hourString + "小时"
                        + miniteString + "分";
                }
                else {
                    if (hour > 0) {
                        str = hourString + "小时"
                            + miniteString + "分";
                    }
                    else {
                        str = miniteString + "分";
                        //+ secondString + Data.getString(110022);
                    }
                }
                return str;
            }
            //2015-02-26
            public static timeToDate(time: number,isShowHM:boolean = true): string {
                var year: string;
                var month: string;
                var date: string;
                var hours: string;
                var minute: string;
                var d: Date = new Date(time * 1000)
                year = d.getFullYear().toString();
    
                if (d.getMonth() < 10)
                    month = "0" + (d.getMonth() + 1)
                else
                    month = (d.getMonth() + 1).toString();
                if (d.getDate() < 10)
                    date = "0" + d.getDate();
                else
                    date = d.getDate().toString()
                if (d.getHours() < 10)
                    hours = "0" + d.getHours();
                else
                    hours = d.getHours().toString();
                if (d.getMinutes() < 10)
                    minute = "0" + d.getMinutes();
                else
                    minute = d.getMinutes().toString();
                if (isShowHM) {
                    return year + "-" + month + "-" + date + " " + hours + ":" + minute
                }
                else
                {
                    return year + "年" + month + "月" + date +"日";
                }
            }
            /**
                 * 将秒数转换为时分秒
                 * @param time
                 * @return 
                 * 
                 */
            public static secondsToString(time: number): string {
                var str: string;
                var _minite: number;
                var hour: number = 0;
                var minite: number = 0;
                var second: number = 0;
    
                second = time % 60;
                _minite = (time - second) / 60;
                minite = _minite % 60;
                hour = (_minite - minite) / 60;
                if (second < 10) {
                    var secondString: String;
                    secondString = "0" + second.toString();
                }
                else {
                    secondString = second.toString();
                }
                if (minite < 10) {
                    var miniteString: String;
                    miniteString = "0" + minite.toString();
                }
                else {
                    miniteString = minite.toString();
                }
                if (hour < 10) {
                    var hourString: String;
                    hourString = "0" + hour.toString();
                }
                else {
                    hourString = hour.toString();
                }
    
                str = hourString + ":" + miniteString + ":" + secondString;
                return str;
            }
            //计算两点间的距离 根据元件
            public static calcDistanceByNode(sp0: egret.Sprite, sp1: egret.Sprite): number {
                var _disX: number = Math.pow(Math.abs(sp0.x - sp1.x), 2);
                var _disY: number = Math.pow(Math.abs(sp0.y - sp1.y), 2);
                var _dist = Math.floor(Math.sqrt(_disX + _disY));
                return _dist;
            }
            //计算两点间的距离 根据两点
            public static calcDistanceByPtArray(startPt: Array<number>, endPt: Array<number>): number {
                var _disX: number = Math.pow(Math.abs(startPt[0] - endPt[0]), 2);
                var _disY: number = Math.pow(Math.abs(startPt[1] - endPt[1]), 2);
                var _dist = Math.floor(Math.sqrt(_disX + _disY));
                return _dist;
            }
                //计算两点间的距离 根据两点
            public static calcDistanceByPt(startPt: egret.Point, endPt: egret.Point): number {
                var _disX: number = Math.pow(Math.abs(startPt.x - endPt.x), 2);
                var _disY: number = Math.pow(Math.abs(startPt.y - endPt.y), 2);
                var _dist = Math.floor(Math.sqrt(_disX + _disY));
                return _dist;
            }
            public static getPointArray(sp: any): Array<number> {
                return [sp.x, sp.y]
            }
            //计算贝塞尔曲线长度
            public static calcBezierLength(pt0: egret.Point, pt1: egret.Point, pt2: egret.Point, t: number): number
            {
                var ax: number = pt0.x - 2 * pt1.x + pt2.x;
                var ay: number = pt0.y - 2 * pt1.y + pt2.y;
                var bx: number = 2 * pt1.x - 2 * pt0.x;
                var by: number = 2 * pt1.y - 2 * pt0.y;
    
                var A: number = 4 * (ax * ax + ay * ay);
                var B: number = 4 * (ax * bx + ay * by);
                var C: number = bx * bx + by * by;
    
                var temp1: number = Math.sqrt(C + t * (B + A * t));
                var temp2: number = (2 * A * t * temp1 + B * (temp1 - Math.sqrt(C)));
                var temp3: number = Math.log(B + 2 * Math.sqrt(A) * Math.sqrt(C));
                var temp4: number = Math.log(B + 2 * A * t + 2 * Math.sqrt(A) * temp1);
                var temp5: number = 2 * Math.sqrt(A) * temp2;
                var temp6: number = (B * B - 4 * A * C) * (temp3 - temp4);
    
                return (temp5 + temp6) / (8 * Math.pow(A, 1.5));
            }
            //两点间角度
            public static calcAngleByPt(pt0: Array<number>, pt1: Array<number>): number {
                //两点的x、y值
                var x: number = pt1[0] - pt0[0];
                var y: number = pt1[1] - pt0[1];
                var hypotenuse: number = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
                //斜边长度
                var cos: number = x / hypotenuse;
                var radian: number = Math.acos(cos);
                //求出弧度
                var angle: number = 180 / (Math.PI / radian);
                //用弧度算出角度        
                if (y < 0) {
                    angle = -angle;
                } else if ((y == 0) && (x < 0)) {
                    angle = 180;
                }
                return angle + 90;
            }
    
            ///玩家名字长度大于len时 截取len-1 的长度 +...
            public static getNameByLength(name: string, len: number): string {
                if (name.length > len) {
                    name = name.substring(0, len - 1) + "...";
                }
                return name;
    
            }
            //欧美计数法
            public static formatReadableNumber(num: number, fraction: number = 0): string
            {
                if (isNaN(num))
                {
                    return "";
                }
                if (num >= 1000000)
                {
                    return Number(num * 0.000001).toFixed(fraction) + "M";
                } else if (num >= 1000)
                {
                    return Number(num * 0.001).toFixed(fraction) + "K";
                }
                return String(num);
            }
            public static formatNumber(num: number): string
            {
                var sign: boolean = true;
                var orig: string = num.toString();
                if (num < 1000)
                {
                    if (!sign) orig = '-' + orig;
                    return orig;
                }
                var result: string = "";
                var dotindex: number = orig.indexOf('.');
                var len: number = 0;
                var remainder: number = 0;
                var nowindex: number = 0;
                if (dotindex >= 0)
                {
                    len = dotindex;
                    remainder = len % 3;
                    if (remainder > 0) result += orig.slice(0, remainder) + ',';
                    nowindex = remainder;
                    while (nowindex < len)
                    {
                        result += orig.slice(nowindex, nowindex + 3);
                        nowindex += 3;
                        if (nowindex < len) result += ',';
                    }
                    result += orig.slice(dotindex, -1);
                } else
                {
                    len = orig.length;
                    remainder = len % 3;
                    if (remainder > 0) result += orig.slice(0, remainder) + ',';
                    nowindex = remainder;
                    while (nowindex < len)
                    {
                        result += orig.slice(nowindex, nowindex + 3);
                        nowindex += 3;
                        if (nowindex < len) result += ',';
                    }
                }
                if (!sign) result = '-' + result;
                return result;
            }
            public static objectLength(obj:Object):number
            {
                var _count:number = 0;
                for(var key in obj)
                {
                    _count++;
                }
                return _count;
            }
        }
    }
    
    //com.elex.utils.load.Utils.assetsUrl = '/assets/popup/';

    3,

    //com.utils.DateUtil.localTimeOffsetMillis = 0;
    //com.utils.DateUtil.server_time = 0;
    //com.utils.DateUtil._server_time = 0;
    //com.utils.DateUtil.MILLIS_PER_HOUR = 3600000;
    //com.utils.DateUtil.MILLIS_PER_MINUTE = 60000;
    //com.utils.DateUtil.MILLIS_PER_SECOND = 1000;
        module utils {
            export class DateUtil extends egret.HashObject {
                public static localTimeOffsetMillis:number = 0;
                public static server_time:number = 0;
                public static _server_time:number = 0;
                //public static _localDate:flash.As3Date;
                public static _localDate: Date;
                //public static get localDate():flash.As3Date
                public static get localDate(): Date
                {
                    if(utils.DateUtil.server_time)
                    {
                        if(utils.DateUtil._server_time == utils.DateUtil.server_time && utils.DateUtil._localDate)
                        {
                            return utils.DateUtil._localDate;
                        }
                        else
                        {
                            utils.DateUtil._server_time = utils.DateUtil.server_time;
                            //utils.DateUtil._localDate = new flash.As3Date(utils.DateUtil.server_time * 1000);
                            utils.DateUtil._localDate = new Date(utils.DateUtil.server_time * 1000);
                        }
                    }
                    else
                    {
                        utils.DateUtil._localDate = null;
                        //utils.DateUtil._localDate = new flash.As3Date();
                        utils.DateUtil._localDate = new Date();
                    }
                    return utils.DateUtil._localDate;
                }
    
                public static setLocalTimeOffsetFromDatabase(_arg1:number)
                {
                    var _local2: number = utils.DateUtil.convertToMillisWithoutOffset(_arg1);
                    //var _local3: number = utils.DateUtil.localDate.time;
                    var _local3: number = utils.DateUtil.localDate.getTime();
                    utils.DateUtil.localTimeOffsetMillis = (_local3 - _local2);
                }
    
                public static convertToMillisWithoutOffset(_arg1:number):number
                {
                    return Number(_arg1);
                }
    
                public static convertDatabaseTimestampToLocalMillis(_arg1:number):number
                {
                    var _local2:number = utils.DateUtil.convertToMillisWithoutOffset(_arg1);
                    var _local3:number = utils.DateUtil.convertDatabaseMillisToLocal(_local2);
                    return (_local3);
                }
    
                public static convertDatabaseMillisToLocal(_arg1:number):number
                {
                    return ((_arg1 + utils.DateUtil.localTimeOffsetMillis));
                }
    
                public static convertMillisToDatabaseDateString(_arg1:number,index:number = 0):string
                {
                    _arg1 = utils.DateUtil.convertLocalMillisToDatabase(_arg1);
                    //var _local2:flash.As3Date = new flash.As3Date();
                    var _local2: Date = new Date();
                    //_local2.time = _arg1;
                    _local2.setTime(_arg1);
                    var _local3: number = (_local2.getMonth() + 1);
                    var _local4:string = ((_local3 < 10))?("0" + _local3):("" + _local3);
                    //var _local5:string = ((_local2.date < 10))?("0" + _local2.date):("" + _local2.date);
                    var _local5: string = ((_local2.getDate() < 10)) ? ("0" + _local2.getDate()) : ("" + _local2.getDate());
                    var _local6: string = ((_local2.getHours() < 10)) ? ("0" + _local2.getHours()) : ("" + _local2.getHours());
                    var _local7: string = ((_local2.getMinutes() < 10)) ? ("0" + _local2.getMinutes()) : ("" + _local2.getMinutes());
                    if(index == 1)
                    {
                        return (((((((((_local2.getFullYear() + "-") + _local4) + "-") + _local5) + " ") + _local6) + ":") + _local7));
                    }
                    else if(index == 2)
                    {
                        return ((((_local2.getFullYear() + "-") + _local4) + "-") + _local5);
                    }
                    var _local8: string = ((_local2.getSeconds() < 10)) ? ("0" + _local2.getSeconds()) : ("" + _local2.getSeconds());
                    return (((((((((((_local2.getFullYear() + "-") + _local4) + "-") + _local5) + " ") + _local6) + ":") + _local7) + ":") + _local8));
                }
    
                public static convertMillisToDatabaseUTCDateString(_arg1:number,index:number = 0):string
                {
                    _arg1 = utils.DateUtil.convertLocalMillisToDatabase(_arg1);
                    //var _local2:flash.As3Date = new flash.As3Date();
                    var _local2: Date = new Date();
                    //_local2.time = _arg1;
                    _local2.setTime(_arg1);
                    var _local3: number = (_local2.getUTCMonth() + 1);
                    var _local4:string = ((_local3 < 10))?("0" + _local3):("" + _local3);
                    //var _local5:string = ((_local2.dateUTC < 10))?("0" + _local2.dateUTC):("" + _local2.dateUTC);
                    var _local5: string = ((_local2.getUTCDate() < 10)) ? ("0" + _local2.getUTCDate()) : ("" + _local2.getUTCDate());
                    var _local6: string = ((_local2.getUTCHours() < 10)) ? ("0" + _local2.getUTCHours()) : ("" + _local2.getUTCHours());
                    var _local7: string = ((_local2.getUTCMinutes() < 10)) ? ("0" + _local2.getUTCMinutes()) : ("" + _local2.getUTCMinutes());
                    if(index == 1)
                    {
                        return (((((((((_local2.getFullYear() + "-") + _local4) + "-") + _local5) + " ") + _local6) + ":") + _local7));
                    }
                    else if(index == 2)
                    {
                        return ((((_local2.getFullYear() + "-") + _local4) + "-") + _local5);
                    }
                    var _local8: string = ((_local2.getUTCSeconds() < 10)) ? ("0" + _local2.getUTCSeconds()) : ("" + _local2.getUTCSeconds());
                    return (((((((((((_local2.getFullYear() + "-") + _local4) + "-") + _local5) + " ") + _local6) + ":") + _local7) + ":") + _local8));
                }
    
                public static localizeTimeLeft(seconds:number):string
                {
                    seconds = Math.floor(seconds);
                    if(seconds < 0)
                    {
                        return "";
                    }
                    var days:number = Math.floor(seconds / 86400);
                    var hours:number = Math.floor((seconds - days * 86400) / 3600);
                    var minutes:number = Math.floor((seconds - days * 86400 - hours * 3600) / 60);
                    seconds = Math.floor((seconds - days * 86400 - hours * 3600 - minutes * 60));
                    var result:string = "";
                    if(days > 0)
                    {
                        result += String(days) + utils.StringUtil.getText("days");
                    }
                    if(hours > 0)
                    {
                        result += String(hours) + utils.StringUtil.getText("hours");
                    }
                    if(minutes > 0)
                    {
                        result += String(minutes) + utils.StringUtil.getText("minutes");
                    }
                    if(seconds > 0)
                    {
                        result += String(seconds) + utils.StringUtil.getText("secondes");
                    }
                    return result;
                }
    
                public static convertLocalMillisToDatabase(_arg1:number):number
                {
                    return _arg1 - (Boolean(utils.DateUtil.localTimeOffsetMillis)?utils.DateUtil.localTimeOffsetMillis:0);
                }
    
                public static getSeverTime():number
                {
                    return Number(Math.floor(utils.DateUtil.convertLocalMillisToDatabase(utils.DateUtil.localDate.getTime()) / 1000));
                }
    
                public static getLeftTime(startTime:number,needTime:number):number
                {
                    var nowTime:number = utils.DateUtil.getSeverTime();
                    var leftTime:number = needTime - (nowTime - Number(startTime));
                    return leftTime;
                }
    
                public static getFanPageNowTime():number
                {
                    //var date:flash.As3Date = new flash.As3Date();
                    var date: Date = new Date();
                    var time:number = Math.floor(date.getTime());
                    return time;
                }
    
                public static setFanPageNowRefreshTime():number
                {
                    //var date:flash.As3Date = new flash.As3Date();
                    var date: Date = new Date();
                    switch(cache.CommonCache.instance.language)
                    {
                    case "zh_CN" :
                        date.setHours(23);
                        date.setMinutes (0);
                        date.setSeconds (0);
                        break;
                    case "zh_TW" :
                        date.setHours(23);
                        date.setMinutes (0);
                        date.setSeconds (0);
                        break;
                    case "de_DE" :
                        date.setHours(16);
                        date.setMinutes (0);
                        date.setSeconds (0);
                        break;
                    case "en_US" :
                        date.setHours(8);
                        date.setMinutes (0);
                        date.setSeconds (0);
                        break;
                    case "ko_KR" :
                        date.setHours(24);
                        date.setMinutes (0);
                        date.setSeconds (0);
                        break;
                    case "ja_JP" :
                        date.setHours(24);
                        date.setMinutes (0);
                        date.setSeconds (0);
                        break;
                    case "pt_BR" :
                        date.setHours(15);
                        date.setMinutes (0);
                        date.setSeconds (0);
                        break;
                    case "th_TH" :
                        date.setHours(22);
                        date.setMinutes (0);
                        date.setSeconds (0);
                        break;
                    case "it_IT" :
                        date.setHours(16);
                        date.setMinutes (0);
                        date.setSeconds (0);
                        break;
                    case "ru_RU" :
                        date.setHours(18);
                        date.setMinutes (0);
                        date.setSeconds (0);
                        break;
                    case "tr_TR" :
                        date.setHours(17);
                        date.setMinutes (0);
                        date.setSeconds (0);
                        break;
                    case "es_ES" :
                        date.setHours(16);
                        date.setMinutes (0);
                        date.setSeconds (0);
                        break;
                    default :
                        break;
                    }
                    var time:number = Math.floor(date.getTime());
                    return time;
                }
    
                public static getDatabaseMillisForRightNow():number
                {
                    var _local1: number = utils.DateUtil.localDate.getTime();
                    return ((_local1 - utils.DateUtil.localTimeOffsetMillis));
                }
    
                public static convertMillisToHHMMSS(_arg1:number):string
                {
                    var _local2:string;
                    var _local3:boolean = (_arg1 < 0);
                    var _local4:number = Math.floor((Math.abs(_arg1) / 1000));
                    var _local5:number = Math.floor(Number((_local4 / 3600)));
                    var _local6: number = Math.floor(Number(((_local4 % 3600) / 60)));
                    var _local7: number = Math.floor(Number((_local4 % 60)));
                    if(((((isNaN(_local5)) || (isNaN(_local6)))) || (isNaN(_local7))))
                    {
                        return ("--:--:--");
                    }
                    var _local8:string = (_local3)?"-":"";
                    var _local9:string = (((_local6 < 10))?"0":"" + _local6);
                    var _local10:string = (((_local7 < 10))?"0":"" + _local7);
                    var _local11:string = ("" + _local5);
                    _local2 = (((((_local8 + _local11) + ":") + _local9) + ":") + _local10);
                    return (_local2);
                }
    
                public static getMonthName(_arg1:Date):string
                {
                    var _local2: Array<any> = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                    return (_local2[_arg1.getMonth()]);
                }
    
                public static getDayName(_arg1:Date):string
                {
                    var _local2: Array<any> = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
                    return (_local2[_arg1.getDate()]);
                }
    
                public static formatTime(_arg1:number):string
                {
                    var day:number = Math.floor(_arg1 / (3600 * 24));
                    var hour:number = Math.floor((_arg1 % (3600 * 24)) / 3600);
                    var min:number = Math.floor((_arg1 / 60) % 60);
                    var sec:number = Math.floor(_arg1 % 60);
                    if(day > 0)
                    {
                        return day + "d " + (hour > 0?hour + ":":"00:") + (min > 9?"":"0") + min + ":" + (sec > 9?"":"0") + sec;
                    }
                    return (hour > 0?hour + ":":"") + (min > 9?"":"0") + min + ":" + (sec > 9?"":"0") + sec;
                }
    
                public static getLeftTimeStr(dt:number):string
                {
                    var timestr:string = "";
                    var timedt:number = 0;
                    if(dt >= 24 * 60 * 60)
                    {
                        timedt = Math.floor(dt / (24 * 60 * 60));
                        timestr = String(timedt);
                        timestr += loader.ResLoader.getValue("105592");
                    }
                    else if(dt >= 60 * 60)
                    {
                        timedt = Math.floor(dt / (60 * 60));
                        timestr = String(timedt);
                        timestr += loader.ResLoader.getValue("105591");
                    }
                    else if(dt >= 60)
                    {
                        timedt = Math.floor(dt / 60);
                        timestr = String(timedt);
                        timestr += loader.ResLoader.getValue("105590");
                    }
                    else
                    {
                        timestr = "1";
                        timestr += loader.ResLoader.getValue("105590");
                    }
                    return timestr;
                }
    
                public static MILLIS_PER_HOUR:number = 3600000;
                public static MILLIS_PER_MINUTE:number = 60000;
                public static MILLIS_PER_SECOND:number = 1000;
                public static getLeftHHMMSS(...args):string
                {
                    var str:string = "";
                    if(args.length == 0)
                    {
                        return "";
                    }
                    var d: Date = utils.DateUtil.getDate(args);
                    var leftT: number = d.getTime() - Math.floor(utils.DateUtil.convertLocalMillisToDatabase(utils.DateUtil.localDate.getTime()));
                    if(leftT < 0)
                    {
                        str = "00:00:00";
                        return str;
                    }
                    var c_hour: number = Math.floor(Number(leftT / utils.DateUtil.MILLIS_PER_HOUR));
                    var _min: number = Math.floor(Number(leftT / utils.DateUtil.MILLIS_PER_MINUTE));
                    var c_min: number = Math.floor(Number(_min % 60));
                    var c_sec: number = Math.floor(Number((leftT - _min * utils.DateUtil.MILLIS_PER_MINUTE) / utils.DateUtil.MILLIS_PER_SECOND));
                    str += c_hour >= 10?"":"0" + c_hour.toString();
                    str += str + ":" + (c_min >= 10?"":"0") + c_min.toString();
                    str += str + ":" + (c_sec >= 10?"":"0") + c_sec.toString();
                    return str;
                }
    
                public static getDate(array:Array<any>):Date
                {
                    var length:number = Math.floor(array.length);
                    if(length == 0)
                    {
                        return new Date();
                    }
                    if(length == 1)
                    {
                        return new Date(array[0]);
                    }
                    if(length > 1)
                    {
                        var d:Date = new Date(2012,0);
                        for(var i:number = 0;i < 7; i++)
                        {
                            if(array[i] != null)
                            {
                                switch(i)
                                {
                                case 0 :
                                    d.setFullYear(array[0]);
                                    break;
                                case 1 :
                                    d.setMonth(array[1]);
                                    break;
                                case 2 :
                                    d.setDate(array[2]);
                                    break;
                                case 3 :
                                    d.setHours(array[3]);
                                    break;
                                case 4 :
                                    d.setMinutes(array[4]);
                                    break;
                                case 5 :
                                    d.setSeconds(array[5]);
                                    break;
                                case 6 :
                                    d.setMilliseconds(array[6]);
                                    break;
                                }
                            }
                        }
                        return d;
                    }
                    return new Date();
                }
    
            }
        }

     4,通过获取秒 来显示 时 分  秒

    public static timeLeftToCountDown(time: number, separator: string = ':'): string {
    var tmpTime: number = Math.floor(time < 0 ? 0 : time);
    var ret: string = '';
    var hours: number = Math.floor(tmpTime / 3600);
    if (hours < 10) {
    ret = ret + "0";
    }
    ret = ret + hours + separator;
    var tmp: number = Math.floor(tmpTime % 3600);
    var minutes: number = Math.floor(tmp / 60);
    if (minutes < 10) {
    ret = ret + "0";
    }
    ret = ret + minutes + separator;
    var seconds: number = Math.floor(tmp % 60);
    if (seconds < 10) {
    ret = ret + "0";
    }
    ret = ret + seconds;
    return ret;
    }

     5, 获取 年月日 时分秒 :

      public static timeStampToYMD(time: number): string {
                //var date: flash.As3Date = new flash.As3Date();
                var date:Date = new Date();
                date.setTime(time);
                var year: number = date.getFullYear();
                var month: number = date.getMonth() + 1;
                var day: number = date.getDate();
                var monthStr: string = String(month);
                if (month < 10) {
                    monthStr = "0" + month;
                }
                var dayStr: string = String(day);
                if (day < 10) {
                    dayStr = "0" + day;
                }
                var result: string = year + "-" + monthStr + "-" + dayStr;
                return result;
            }
    
    public static timeStampToDHM(time: number): string {
                //var date: flash.As3Date = new flash.As3Date(time);
                var date: Date = new Date(time);
                var hour: number = date.getHours();
                var minute: number = date.getMinutes();
                var hourStr: string = String(hour);
                if (hour < 10) {
                    hourStr = "0" + hour;
                }
                var minuteStr: string = String(minute);
                if (minute < 10) {
                    minuteStr = "0" + minute;
                }
                var result: string = timeStampToYMD(time);
                result += "  " + hourStr + ":" + minuteStr;
                return result;
            }

     2,有时显示的时间中,最后一位的秒数显示的是很多位的上万的形式,只要在改变量的前面加上 Math.floor来控制一下就好了;

  • 相关阅读:
    SQL通用数据类型
    SQL基础
    软件测试相关(1)
    C语言——判断
    C语言新手教程——计算
    并查集
    洛谷-P1551 亲戚
    洛谷-P1536 村村通
    洛谷-P1525 [NOIP2010 提高组] 关押罪犯
    洛谷-P2814 家谱
  • 原文地址:https://www.cnblogs.com/RitaLee/p/6525494.html
Copyright © 2020-2023  润新知