如何在Javascript格式化日期?

时间:2015-12-06 20:35:23

标签: javascript

我试图在JS中格式化日期,如下所示格式化为Y-m-d但是我想得到 ymdms

datestr = current.toISOString().split('T')[0];
resulting_dates.push(datestr);//dates.push(d.toISOString().split('T')[0]);

1 个答案:

答案 0 :(得分:1)

以下是一个工作示例,您可以修改日期以获得首选格式:

jsfiddle

<button onclick="myFunction();">Try it</button>

<p id="demo"></p>

<script>

function myFunction() {
var currentdate = new Date(); 
var currentdate = new Date(); 
var datetime = currentdate.getDate() + "/"+ (currentdate.getMonth()+1)  + "/" + currentdate.getFullYear() + " "+ currentdate.getHours() + ":"  + currentdate.getMinutes() + ":" + currentdate.getSeconds();
document.getElementById("demo").innerHTML = datetime;
}

</script>