html input日期值,input标签设置时间值
研究了兩天javascript日期相關的內容,一句話總結:“瀏覽器可以用上十萬年,因為javascript支持的時間范圍就是這么長”。
通過實驗來看,比較好用的是type="date",及type="datetime-local"
關于設置值的結論:
(1)2020-03-26格式可用于date、datetime
(2)2020-03-26 22:32:07格式僅可以用于datetime
(3)2020-03-26T22:33:03格式可以用于datetime、datetime-local
關于輸入及校驗日期的正確性:能輸入不代表就能正確轉換成日期
代碼:沒寫過WEB程序,算是個練習,將就著看吧。
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size: 11px;
color: #333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
text-align: center;
}
table.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
function FormatDate() {
//var current = new Date();
var now = new Date();
var year = now.getFullYear();
//month返回數組(0-11)
var month = ("0" + (now.getMonth() + 1)).slice(-2);
//getDate返回日期(1-31)
var day = ("0" + now.getDate()).slice(-2);
var formatedDate = year + "-" + month + "-" + day;
//alert("系統時間:" + now + "\n格式化時間:" + formatedDate);
return formatedDate;
}
function FormatDate1() {
//var current = new Date();
var now = new Date();
var year = now.getFullYear();
//month返回數組(0-11)
var month = ("0" + (now.getMonth() + 1)).slice(-2);
//getDate返回日期(1-31)
var day = ("0" + now.getDate()).slice(-2);
var formatedDate = year + "/" + month + "/" + day;
//alert("系統時間:" + now + "\n格式化時間:" + formatedDate);
return formatedDate;
}
function FormatDateTime() {
//var current = new Date();
var now = new Date();
var year = now.getFullYear();
//month返回數組(0-11)
var month = ("0" + (now.getMonth() + 1)).slice(-2);
//getDate返回日期(1-31)
var day = ("0" + now.getDate()).slice(-2);
//
var hour = ("0" + now.getHours()).slice(-2);
var minute = ":" + ("0" + now.getMinutes()).slice(-2);
var second = ":" + ("0" + now.getSeconds()).slice(-2);
var formatedDate = year + "-" + month + "-" + day + " " + hour + minute + second;
//alert("系統時間:" + now + "\n格式化時間:" + formatedDate);
return formatedDate;
}
function FormatDateTimeLocal() {
//var current = new Date();
var now = new Date();
var year = now.getFullYear();
//month返回數組(0-11)
var month = ("0" + (now.getMonth() + 1)).slice(-2);
//getDate返回日期(1-31)
var day = ("0" + now.getDate()).slice(-2);
//
var hour = ("0" + now.getHours()).slice(-2);
var minute = ":" + ("0" + now.getMinutes()).slice(-2);
var second = ":" + ("0" + now.getSeconds()).slice(-2);
var formatedDate = year + "-" + month + "-" + day + "T" + hour + minute + second;
return formatedDate;
}
input標簽typeinput標簽輸入格式測試
input type="date"
input type="datetime"
input type="datetime-local"
function setDate1() {
var str = FormatDate();
$("#lb00001").prop("innerHTML", str);
$('#mydate00001').prop('value',str );
$('#mydate00002').prop('value', str);
$('#mydate00003').prop('value', str)
}
function setDate2() {
var str = FormatDateTime();
$("#lb00002").prop("innerHTML", str);
$('#mydate00001').prop('value', str);
$('#mydate00002').prop('value', str);
$('#mydate00003').prop('value', str);
}
function setDate3() {
var str = FormatDateTimeLocal();
$("#lb00003").prop("innerHTML", str);
$('#mydate00001').prop('value',str);
$('#mydate00002').prop('value', str);
$('#mydate00003').prop('value', str);
}
function dateTest() {
var result="";
$("input[type='date']")
.each(function () {
var v = new Date($(this).val());
result += "\ndate取值:" + $(this).val() + "\n日期時間轉換后的值:" + v
}
);
$("input[type='datetime']")
.each(function () {
var v = new Date($(this).val());
result += "\ndatetime取值:" + $(this).val() + "\n日期時間轉換后的值:" + v
}
);
$("input[type='datetime-local']")
.each(function () {
var v = new Date($(this).val());
result += "\ndatetime-local取值:" + $(this).val() + "\n日期時間轉換后的值:" + v
}
);
alert(result);
};
標簽:slice,設置,month,標簽,prop,str,var,input,now
來源: https://www.cnblogs.com/lnwuyaowei/p/12578090.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的html input日期值,input标签设置时间值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: red hat enterprise l
- 下一篇: Command 传参的几种方式