datetime-local 设置日期格式

2025-10-01 01:43:44

1、代码如下:

<html>

<title>Test_datetime_local</title>

<body>

 datatime-local:<input type="datetime-local" id="mydatetime-local"/>

<br>date:<input type="date" id="mydate"/>

<br>week:<input type="week" id="myweek"/>

<br>month:<input type="month" id="mymonth"/>

<br>time:<input type="time" id="mytime"/>

<br>datetime:<input type="datetime" id="mydatetime"/>

Click the button and set the local datetime

<button onclick="myFunction()">Click here</button>

<p id="demo">

<script>

function myFunction(){

    var x=document.getElementById("mydatetime-local").value;

    year=x.substring(0,4);

    month=x.substring(5,7);

    day=x.substring(8,10);

    hour=x.substring(11,13);

    minute=x.substring(14)

    format=year+"-"+day+"-"+month;

    document.getElementById("demo").innerHTML = format;

}

</script>

</body>

</html>

保存为.js文件并用浏览器打开

datetime-local 设置日期格式

2、打开后结果如下:一般不要使用firefox或者IE,使用其他浏览器,如百度浏览器。因为其他浏览器可能存在问题,显示效果比较差。

接下来设置日期,

datetime-local 设置日期格式

3、选择年月日:

点击下拉按钮即可得到如下界面。

选择日期,这里选择2016年10月31日

datetime-local 设置日期格式

4、选择时间,必选:

点击上下调整按钮对时间进行调整

这里调整为00:59

datetime-local 设置日期格式

5、点击Click here 按钮:

结果显示2016-31-10

即是年-日-月格式

(yyyy-dd-mm)

datetime-local 设置日期格式

6、此函数用于格式化时间。

function myFunction(){

    var x=document.getElementById("mydatetime-local").value;

    year=x.substring(0,4);

    month=x.substring(5,7);

    day=x.substring(8,10);

    hour=x.substring(11,13);

    minute=x.substring(14)

    format=year+"-"+day+"-"+month;

    document.getElementById("demo").innerHTML = format;

}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢