如何在JavaScript中获取当前日期? [英] How do I get the current date in JavaScript?
本文介绍了如何在JavaScript中获取当前日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在JavaScript中获取当前日期?
How do I get current date in JavaScript?
推荐答案
Hendrik在上述评论中的答案将会起作用,但可能不会你在找什么格式不是很有用。
Hendrik's answer in the comments above will work but probably isn't what you are looking for. The format is not very usable.
我已经有同样的问题,并通过大量的搜索得出了这一点。
I have been having the same issue and have come up with this through a lot of searching.
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = mm+'/'+dd+'/'+yyyy;
document.write(today);
这是非常复杂的,但它将以mm / dd / yyyy格式给您今天的日期。
It's quite complex but it will give you today's date in the format of mm/dd/yyyy.
只需更改 today = mm +' /'+ dd +'/'+ yyyy;
到你想要的格式。
Simply change today = mm+'/'+dd+'/'+yyyy;
to what ever format you wish.
这篇关于如何在JavaScript中获取当前日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文