我想知道如何在jQuery中使用Date()函数以yyyy/mm/dd格式获取当前日期。


当前回答

我知道我迟到了,但这就是你所需要的

var date = (new Date()).toISOString().split('T')[0];

toISOString()使用javascript的内置函数。

cd = (new Date()).toISOString().split('T')[0]; console.log (cd); 警报(cd);

其他回答

var today = new Date(); var tday = getfullyear () + '/' + ( today.getMonth() < 9 ?'0': " ) + (today.getMonth() + 1) + '/' + ( getdate () < 10 ?'0': " ) + today.getDate(); console.log (tday);

你也可以用moment.js实现这一点。 在你的html中包含moment.js。

<script src="moment.js"></script>

并在脚本文件中使用下面的代码来获得格式化日期。

moment(new Date(),"YYYY-MM-DD").utcOffset(0, true).format();

看到这个。 $.now()方法是表达式(new Date). gettime()返回的数字的简写。

试试这个…

var d = new Date();
alert(d.getFullYear()+'/'+(d.getMonth()+1)+'/'+d.getDate());

getMonth()返回月份0到11,因此我们希望添加1来表示准确的月份

参考:https://www.w3schools.com/jsref/jsref_obj_date.asp

因为问题被标记为jQuery:

如果你也在使用jQuery UI,你可以使用$.datepicker.formatDate():

$.datepicker.formatDate('yy/mm/dd', new Date());

请看这个演示。