js获取当前时间方法

js获取当前时间

html代码

<div id="current"></div>

JS代码

$("#current").html(currentDate(true, true))

/*
type:true(格式:2018年01月01日)
	false(格式:2018-01-01)
isno: true(包含时分秒:2018-01-01 01:01:01)
	false(不包含时分秒:2018-01-01)
*/
function currentDate(type, isno) {
    var date = new Date();
    var year = singular(date.getFullYear());
    var month = singular(date.getMonth() + 1);
    var strDate = singular(date.getDate());
    var hours = singular(date.getHours());
    var minutes = singular(date.getMinutes());
    var seconds = singular(date.getSeconds());
    if (type) {
        if (isno) {
            return year + "年" + month + "月" + strDate + "日 " + hours + "时" + minutes + "分" + seconds + "秒";
        } else {
            return year + "年" + month + "月" + strDate + "日";
        }
    } else {
        if (isno) {
            return year + "-" + month + "-" + strDate + " " + hours + ":" + minutes + ":" + seconds;
        } else {
            return year + "-" + month + "-" + strDate;
        }
    }
}
// 单数补位
function singular(num) {
    if (num < 10) {
        num = "0" + num;
    }
    return num;
}

「点点赞赏,手留余香」

1

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:
1. 本站所有文章教程及资源素材均来源于网络与用户分享或为本站原创,仅限用于学习和研究。
2. 如果内容损害你的权益请联系客服QQ:1642748312给予处理。
码云笔记 » js获取当前时间方法

发表回复

IT互联网行业相关广告投放 更专业 更精准

立即查看 联系我们