获取当前日期时间并格式化 - 常见格式

获取系统当前日期时间的方法:
 1 //获取系统当前日期时间
 2 Date.prototype.format = function (format) {
 3     var o = {
 4         "M+": this.getMonth() + 1, //month
 5         "d+": this.getDate(), //day
 6         "h+": this.getHours(), //hour
 7         "m+": this.getMinutes(), //minute
 8         "s+": this.getSeconds(), //second
 9         "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
10         "S": this.getMilliseconds() //millisecond
11     };
12 
13     if (/(y+)/.test(format)) {
14         format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
15     }
16 
17     for (var k in o) {
18         if (new RegExp("(" + k + ")").test(format)) {
19             format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
20         }
21     }
22     return format;
23 }

使用方法1:

var now = new Date();
var nowStr = now.format("yyyy-MM-dd hh:mm:ss");

使用方法2:

var testDate = new Date();
var testStr = testDate.format("yyyy年MM月dd日hh小时mm分ss秒");
console.log(nowStr);
console.log(testStr);

使用方法3:

console.log(new Date().format("yyyy年MM月dd日"));
console.log(new Date().format("MM/dd/yyyy"));
console.log(new Date().format("yyyyMMdd"));
console.log(new Date().format("yyyy-MM-dd hh:mm:ss"));
light7 框架使用日期时间模块,对其限制时间做小为当前时间:
$(function () {
    $(document).on("pageInit", function () {
        $('input[name="newSignDate"]').datetimePicker({
            toolbarTemplate: '<header class="bar bar-nav">\
            <button class="button button-link pull-right close-picker">确定</button>\
            <h1 class="title">选择日期和时间</h1>\
            </header>'
        }).val(function () {
            return new Date().format("yyyy-MM-dd hh:mm:ss");
        }).bind('change', function () {
            var _date = new Date().format("yyyy-MM-dd hh:mm:ss");
            console.log($(this).val());
            if ($(this).val() <= _date) {
                $.alert("签收时间必须大于当前时间")
            }
        })
    });
    $.init();
})

转载于:https://www.cnblogs.com/myclovers/p/5394722.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值