JavaScript📌小技巧

182 阅读1分钟

1.BootStrap中的 data-toggle / data-target 触发问题

问题:当标签内有 data-toggle / data-target / area-hiddenBootStrap属性时,很难使用 JavaScript / jQuery 来进行增删。 解决方法:使用 $('').click() 方法触发它本身的事件即可。

2.时间戳与时间互转

//时间戳转换
Date.prototype.format = function (format) {
    var date = {
        "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+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
                date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
    }
    return format;
}
function TimeDate(time, bo) {
    var today = new Date();
    today.setHours(0);
    today.setMinutes(0);
    today.setSeconds(0);
    today.setMilliseconds(0);
    today = today.getTime() / 1000;
    var nowtime = Math.ceil(new Date().getTime() / 1000);
    var newDate = new Date();
    newDate.setTime(time * 1000);
    return newDate.format(bo);
}
$('#elementId').val(TimeDate(elementTime, 'yyyy-MM-dd'))
$('.elementClass').val(TimeDate(elementTime, 'yyyy-MM-dd'))
// 时间转时间戳
var birthday = Date.parse($('[name=birthday]').val()) / 1000