工具函数

Home文章
 简介:平时自己写的一些工具函数

希望用到,但是网上没找到的工具函数

时间比较函数,注意有兼容性问题

function diffdate(d){ var diff = new Date().getTime()-new Date(d).getTime(); var getk = function(s,u){ var tem = (diff/s)|0; return tem?tem+u:'';} var Y = getk(12*30*24*60*60*1000,'年') var M = getk(30*24*60*60*1000,'月') var D = getk(24*60*60*1000,'日') var h = getk(60*60*1000,'小时') var m = getk(60*1000,'分钟') var s = getk(1000,'秒') return (Y||M||D||h||m||s)+'前'; } diffdate('2016-05-02 11:51'); diffdate('2016-05-02 02:01:45'); diffdate('2016-05-01 02:01:45'); diffdate('2016-03-01 02:01:45'); diffdate('2015-03-01 02:01:45'); console.log(diffdate('1991-12-21 12:01:45'),'YOU出生');

格式化大小

function prettysize(s){ return (s/1024/1024>1)?(s/1024/1024).toFixed(2)+'M':(s/1024).toFixed(2)+'KB' }

 

 

获取今天,或者相对今天的日期

    function getseday(n){
        var offs = n || 0;
        var now = new Date();
        now.setDate(now.getDate() + offs);
        var today = [now.getFullYear(),now.getMonth()+1,now.getDate()];
        if(today[1]<10){
            today[1] = '0'+today[1];
        }
        if(today[2]<10){
            today[2] = '0'+today[2];
        }
        //console.log(today.join('-'));
        return today.join('-');
    }

getseday(0)
//2016-11-10
getseday(1)
//2016-11-11
getseday(-1)
//2016-11-09

 



评论
Login