js处理时间

var date = new Date();

var d = new Date(milliseconds); // 参数为毫秒 参数946656000000 输出 Sat Jan 01 2000 00:00:00 GMT+0800 (GMT+08:00)

var d = new Date(dateString); // 参数 2000-01-02 输出 Sun Jan 02 2000 08:00:00 GMT+0800 (GMT+08:00)

var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

new Date().getYear();                   // 获取当前年份后2位 (已废弃)
new Date().getFullYear();               // 获取完整的年份(4位,1970-至今)
new Date().getMonth();                  // 获取当前月份(0-11,0代表1月)
new Date().getDate();                   // 获取当前日(1-31)
new Date().getDay();                    // 获取当前星期(0-6,0代表星期天)
new Date().getTime();                   // 获取当前时间(从1970.1.1 开始的毫秒数)  
new Date().getHours();                  // 获取当前小时数(0-23)
new Date().getMinutes();                // 获取当前分钟数(0-59)
new Date().getSeconds();                // 获取当前秒数(0-59)
new Date().getMilliseconds();           // 获取当前毫秒数(0-999)
new Date().toLocaleDateString();        // 获取当前日期 (输出结果:2022/2/2)
new Date().toLocaleTimeString();        // 获取当前时间 (输出结果:10:41:30)
new Date().toLocaleString();            // 获取日期与时间 (输出结果:2022/2/2 10:41:30) 
(new Date()).Format("Y-m-d H:i:s")

Date.prototype.Format = function (format) {
    const year = this.getMonth() + 1;
    const month = this.getMonth() + 1;
    const day = this.getDate();
    const hour = this.getHours();
    const minute = this.getMinutes()
    const sec = this.getSeconds()
    var o = {
        "Y": year,
        "y": year.substring(year.length-2,year.length),
        "m": month>10?month:"0"+month,
        "d": day>0?day:"0"+day,
        "H": hour>0?hour:"0"+hour,
        "h": hour/2>0?hour/2:"0"+hour/2,
        "i": minute>0?minute:"0"+minute,
        "s": sec>0?sec:"0"+sec,
    };
    for (var k in o){
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1,o[k]);
    }
    return fmt;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值