您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JS实现时间格式化的方式汇总_javascript技巧

2024/2/23 20:12:30发布17次查看
复制代码 代码如下:
//扩展date的format方法
date.prototype.format = function (format) {
    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(format)) {
        format = format.replace(regexp.$1, (this.getfullyear() + ).substr(4 - regexp.$1.length));
    }
    for (var k in o) {
        if (new regexp(( + k + )).test(format)) {
            format = format.replace(regexp.$1, regexp.$1.length == 1 ? o[k] : (00 + o[k]).substr(( + o[k]).length));
        }
    }
    return format;
}
/** 
*转换日期对象为日期字符串 
* @param date 日期对象 
* @param isfull 是否为完整的日期数据, 
*               为true时, 格式如2000-03-05 01:05:04 
*               为false时, 格式如 2000-03-05 
* @return 符合要求的日期字符串 
*/ 
function getsmpformatdate(date, isfull) {
    var pattern = ;
    if (isfull == true || isfull == undefined) {
        pattern = yyyy-mm-dd hh:mm:ss;
    } else {
        pattern = yyyy-mm-dd;
    }
    return getformatdate(date, pattern);
}
/** 
*转换当前日期对象为日期字符串 
* @param date 日期对象 
* @param isfull 是否为完整的日期数据, 
*               为true时, 格式如2000-03-05 01:05:04 
*               为false时, 格式如 2000-03-05 
* @return 符合要求的日期字符串 
*/  function getsmpformatnowdate(isfull) {
    return getsmpformatdate(new date(), isfull);
}
/** 
*转换long值为日期字符串 
* @param l long值 
* @param isfull 是否为完整的日期数据, 
*               为true时, 格式如2000-03-05 01:05:04 
*               为false时, 格式如 2000-03-05 
* @return 符合要求的日期字符串 
*/ 
function getsmpformatdatebylong(l, isfull) {
    return getsmpformatdate(new date(l), isfull);
}
/** 
*转换long值为日期字符串 
* @param l long值 
* @param pattern 格式字符串,例如:yyyy-mm-dd hh:mm:ss 
* @return 符合要求的日期字符串 
*/ 
function getformatdatebylong(l, pattern) {
    return getformatdate(new date(l), pattern);
}
/** 
*转换日期对象为日期字符串 
* @param l long值 
* @param pattern 格式字符串,例如:yyyy-mm-dd hh:mm:ss 
* @return 符合要求的日期字符串 
*/ 
function getformatdate(date, pattern) {
    if (date == undefined) {
        date = new date();
    }
    if (pattern == undefined) {
        pattern = yyyy-mm-dd hh:mm:ss;
    }
    return date.format(pattern);
}
//alert(getsmpformatdate(new date(1279849429000), true));
//alert(getsmpformatdate(new date(1279849429000),false));   
//alert(getsmpformatdatebylong(1279829423000, true));
//alert(getsmpformatdatebylong(1279829423000,false));   
//alert(getformatdatebylong(1279829423000, yyyy-mm));
//alert(getformatdate(new date(1279829423000), yy-mm));
//alert(getformatdatebylong(1279849429000, yyyy-mm hh:mm));
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product