js日期格式化

方法一:

Date.prototype.Format = function (fmt) { //author: meizz
        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(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

调用:

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

var time2 = new Date().Format("yyyy-MM-dd");

 

//格式化日期

function formatDate() {
    var value2 = new Date();
    var year = value2.getFullYear();
    var month = value2.getMonth() + 1 < 10 ? "0" + (value2.getMonth() + 1) : value2.getMonth() + 1;
    var day = value2.getDate() < 10 ? "0" + value2.getDate() : value2.getDate();
    var currentDate = year + "-" + month + "-" + day;
    return currentDate;
}

 

2、input禁止键盘及中文输入,但可以点击


<input>禁止键盘及中文输入,但又不能用readonly 而且还需兼容ie 和 ff , 为了完成这功能费了蛮大功夫,呵呵,在此记录以便日后之用;另外禁止粘贴 onpaste="return false"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script>
// 样式 style="ime-mode:disabled" 禁止中文输入
function noPermitInput(e){
var evt = window.event || e ;
if(isIE()){
evt.returnValue=false; //ie 禁止键盘输入
}else{
evt.preventDefault(); //fire fox 禁止键盘输入
}
}
function isIE() {
if (window.navigator.userAgent.toLowerCase().indexOf("msie") >= 1)
return true;
else
return false;
}
</script>
</HEAD>
<BODY>
禁止键盘按键输入及中文输入
<input type="text" value="" style="ime-mode:disabled" onkeypress="noPermitInput(event)" >
</BODY>
</HTML>

ime-mode    CSS提议属性
语法:
ime-mode : auto | active | inactive | disabled
参数:
auto : 不影响IME的状态。与不指定ime-mode属性时相同
active : 指定所有使用IME输入的字符。即激活本地语言输入法。用户仍可以撤销激活IME
inactive : 指定所有不使用IME输入的字符。即激活非本地语言。用户仍可以撤销激活IME
disabled : 完全禁用IME。对于有焦点的控件(如输入框),用户不可以激活IME

文本框里面不能手动输入
<input type="text" value="设置仅可读" style="cursor:hand" readonly>
<input type="text" value="设置无法获取" style="cursor:hand" onfocus="this.blur()">

 

posted on 2017-10-07 13:51  好好学习,天天睡觉  阅读(158)  评论(0编辑  收藏  举报