JavaScript日期和时间格式, [英] JavaScript date and time formatting,

查看:196
本文介绍了JavaScript日期和时间格式,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于这个主题的帖子,但是我并没有太多的编程方面的内容......我做了一些测试,但从来没有达到我想要的。



这里是我要编辑的代码:

 < p id =DATE>< / p> 
< script>
var d = new Date();
document.getElementById(DATE)。innerHTML = d.toString();
< / script>

它给我日期OK ...但不是我想要的格式...
我想要的格式是... DD-MM-YYYY HH:MM



简单的就是:)

它已经有几个小时了,我不能这样做...任何人都可以帮助一个代码,我只是可以复制/粘贴在我的HTML文件。

解决方案

与使用外部JavaScript库进行简单任务相反。你应该能够达到与香草js相同。



请参考:



总之:

  var d = new Date(); 
var curr_date = d.getDate();
var curr_month = d.getMonth()+ 1; //月份基于零
var curr_year = d.getFullYear();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
console.log(curr_date + - + curr_month + - + curr_year ++ curr_hour +:+ curr_min);

上面的代码片段应该包含所有您需要的代码。



对于您的具体实现,您需要执行以下操作:

 < script> 
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth()+ 1; //月份基于零
var curr_year = d.getFullYear();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
var formattedDate = curr_date + - + curr_month + - + curr_year ++ curr_hour +:+ curr_min;
document.getElementById(DATE)。innerHTML = formattedDate;
< / script>


I have read a lot of thread about the subject, but i'm not much into programming... i've made some tests but never acheive what i wanted.

Here we have a html template we have to modify to suit our needs.

here the code i have to edit:

<p id="DATE"></p>
<script>
    var d = new Date();
    document.getElementById("DATE").innerHTML = d.toString();
</script>

Its giving me the date OK...but not i the format i want... the format i want is... DD-MM-YYYY HH:MM

Simple has that :)

Its been a couple of hours and i can't do it... Can anyone help with a code i just can copy/paste in my html file.

解决方案

As opposed to using an external JavaScript library for a simple task. You should be able to achieve the same with Vanilla js.

Refer to this: Where can I find documentation on formatting a date in JavaScript?

In short:

var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
console.log(curr_date + "-" + curr_month + "-" + curr_year + " " + curr_hour + ":" + curr_min);

The above snippet should have all the code you need.

For your specific implementation, you will need to do the below:

<script>
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1; //Months are zero based
    var curr_year = d.getFullYear();
    var curr_hour = d.getHours();
    var curr_min = d.getMinutes();
    var formattedDate = curr_date + "-" + curr_month + "-" + curr_year + " " + curr_hour + ":" + curr_min;
    document.getElementById("DATE").innerHTML = formattedDate;
</script>

这篇关于JavaScript日期和时间格式,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆